Questions tagged [tuples]

In programming, tuples are simple *product types*, representing ordered collections of types.

A tuple, like a struct in some languages, is a collection of items of any type delimited with parentheses. Unlike structs, however, tuples are immutable. Many programming languages have "taken" tuples from functional programming languages like F#, where they are used very often.

In Python, for example, tuples are used to encapsulate the actual parameters of functions and methods. Tuples are related to currying of functions, which is a transformation that turns a function taking a tuple with n fields, into a n functions each taking one argument.

Creating a tuple

>>> l = [1, 'a', [28, 3.5]] #square brackets
>>> t = (1, 'a', [28, 3.5]) #round parentheses 
>>> type(t), type(l)
(<class 'tuple'>, <class 'list'>)
>>> t
(1, 'a', [6, 3.5])
>>> tuple(l)
(1, 'a', [6, 3.5])
>>> t == tuple(l)
True
>>> t == l
False

Also the statement

t = 12345, 54321, 'hello!'
 

is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible, e.g.:

>>> brian, x_ray, lemba = t

This is called, appropriately enough, tuple unpacking. Tuple unpacking requires that there be the same number of variables as items in tuple. Note that multiple assignment is really just a combination of tuple packing and tuple unpacking!

A one item tuple is created by an item in parens followed by a comma: e.g

>>> t = ('A single item tuple',)
>>> t
('A single item tuple',)

Also, tuples will be created from items separated by commas, like so:

>>> t = 'A', 'tuple', 'needs', 'no', 'parens'
>>> t
('A', 'tuple', 'needs', 'no', 'parens')

References

11941 questions
4
votes
5 answers

Python replace tuples from list of tuples by highest priority list

I have those python lists : x = [('D', 'F'), ('A', 'D'), ('B', 'G'), ('B', 'C'), ('A', 'B')] priority_list = ['A', 'B', 'C', 'D', 'F', 'G'] # Ordered from highest to lowest priority How can I, for each tuple in my list, keep the value with the…
Waroulolz
  • 297
  • 9
  • 23
4
votes
5 answers

How can I print items from a tuple?

I got the tuple - Result = [ ('80407', 'about power supply of opertional amplifier', '11 hours ago'), ('80405', '5V Regulator Power Dissipation', '11 hours ago')] I want to iterate over the tuples and separate the items in the tuples by…
Alex_P
  • 2,580
  • 3
  • 22
  • 37
4
votes
1 answer

Map list of tuples into list of second elements in Groovy?

I have: List> listOfTuples I would like to have a list of second elements from each Tuple2 Eg. [Tuple2('1','foo'), Tuple2('2','bar')] to ['foo','bar']
pixel
  • 24,905
  • 36
  • 149
  • 251
4
votes
4 answers

is there a data structure equivalent to a non-unique set in python?

I have a very large list of lists of integers which I would like to "hash()" to improve search speed. The resulting hashed value for each of the nested lists needs to be independent of the order of the integers and only on the values in the list. …
Andy
  • 41
  • 1
  • 2
4
votes
3 answers

How to separate with commas an unpacked list of tuples

Unpacking the resulting list of tuples into a comma-separated values. Using FuzzyWuzzy, I am comparing 2 files and want to output the results into a 3rd file. Building out from this SO question: Python: Keepning only the outerloop max result…
4
votes
2 answers

Best data structure to store an object indexed by a 3-tuple for fast retrieval along each dimension and low memory profile?

I want to store objects indexed by a 3-tuple of (String, String , DateTime). Lets call these Identifier,Category,Day Any object in the data structure is guaranteed to be unique by the 3-tuple (no duplicates) The data structure should support fast…
Suraj
  • 35,905
  • 47
  • 139
  • 250
4
votes
5 answers

Nested structure: List of lists of tuples python

I am trying to deal with a nested structure that looks like this: list_of_lists= [[("aaa"),("bbb")],[("ccc"),("ddd")],[("eee"),("fff")]] and I need to add a column of elements that looks like this: column_to_add = ["string1", "string2",…
iraciv94
  • 782
  • 2
  • 11
  • 26
4
votes
1 answer

Build DataFrame from list of

How do you build a pandas DataFrame from a list of pandas.core.frame.Pandas elements? Code example illustrating question: I have the DataFrame below: import pandas as pd df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]}, index=['a',…
Andy
  • 919
  • 2
  • 9
  • 22
4
votes
3 answers

How to cast namedtuple into tuple

How to do the opposite of this? I have a list of namedtuples that I built by iterating a pandas dataframe: list = [] for currRow in dataframe.itertuples(): list.append(currRow) How to convert this list of namedtuples into a list of tuples? Note…
PlsWork
  • 1,958
  • 1
  • 19
  • 31
4
votes
4 answers

Find all items with the top 5 unique values based upon 2nd element in tuple list

I want to find the all tuple items with the top 5 max values in a tuple list based upon the 2nd element of the tuple. For example, I have a tuple list x1 = [(a, 5), (b, 5), (c, 4), (d, 3), (e, 8), (f, 9), (g, 2), (h, 1)] I want to get the…
MC X
  • 337
  • 4
  • 16
4
votes
1 answer

How do I make a dynamic index to get the value of a tuple?

In Python, I learned that I can retrieve a tuple value by a dynamic index: data = (1,2,3,4) data[0] for a in range(len(data)): print(data[a]) The output: 1 2 3 4 how to do this in Rust? I tried something like this: fn main() { let data =…
Arian Saputra
  • 356
  • 6
  • 15
4
votes
4 answers

return tuple with two arrays

I am trying to call a function which returns a tuple with two arrays. The content of the arrays are based on checked items in a checkedListBox. I define the arrays and call the function "storeParametersInArrays" as shown below. string[]…
ChrisRun
  • 131
  • 1
  • 9
4
votes
1 answer

Inserting std::tuple into the std::map

Following example code doesn't compile, I'm not able to figure out how to insert an int and tuple into the map. #include #include #include int main() { std::map> map; …
user11157650
4
votes
2 answers

Python: How to write a dictionary of tuple values to a csv file?

How do I print the following dictionary into a csv file? maxDict = {'test1': ('alpha', 2), 'test2': ('gamma', 2)} So, that the output CSV looks as follows: test1, alpha, 2 test2, gamma, 2
siva
  • 2,105
  • 4
  • 20
  • 37
4
votes
1 answer

C++17 Tuple Unpacking

I am trying to unpack a tuple and use the results to assign into members. Is it possible to do that in idiomatic C++17? I realize that std::tie exists but I am trying to utilize C++17 features and not default to older features nor the old way…