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
98
votes
3 answers

How to reverse tuples in Python?

Is this possible? Doesn't have to be in place, just looking for a way to reverse a tuple so I can iterate on it backwards.
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
97
votes
6 answers

Accessing a value in a tuple that is in a list

[(1,2), (2,3), (4,5), (3,4), (6,7), (6,7), (3,8)] How do I return the 2nd value from each tuple inside this list? Desired output: [2, 3, 5, 4, 7, 7, 8]
super9
  • 29,181
  • 39
  • 119
  • 172
97
votes
8 answers

Why is tuple faster than list in Python?

I've just read in "Dive into Python" that "tuples are faster than lists". Tuple is immutable, and list is mutable, but I don't quite understand why tuple is faster. Anyone did a performance test on this?
Delta76
  • 13,931
  • 30
  • 95
  • 128
95
votes
11 answers

how to add value to a tuple?

I'm working on a script where I have a list of tuples like ('1','2','3','4'). e.g.: list = [('1','2','3','4'), ('2','3','4','5'), ('3','4','5','6'), ('4','5','6','7')] Now I need to add '1234', '2345','3456' and '4567'…
Shahzad
  • 951
  • 1
  • 6
  • 3
95
votes
13 answers

What requirement was the tuple designed to solve?

I'm looking at the new C# feature of tuples. I'm curious, what problem was the tuple designed to solve? What have you used tuples for in your apps? Update Thanks for the answers thus far, let me see if I have things straight in my mind. A good…
Chaddeus
  • 13,134
  • 29
  • 104
  • 162
95
votes
14 answers

Convert a Scala list to a tuple?

How can I convert a list with (say) 3 elements into a tuple of size 3? For example, let's say I have val x = List(1, 2, 3) and I want to convert this into (1, 2, 3). How can I do this?
grautur
  • 29,955
  • 34
  • 93
  • 128
93
votes
4 answers

C# 7 tuples and lambdas

With new c# 7 tuple syntax, is it possible to specify a lambda with a tuple as parameter and use unpacked values inside the lambda? Example: var list = new List<(int,int)>(); normal way to use a tuple in lambda: list.Select(value => value.Item1*2 +…
Rast
  • 2,341
  • 4
  • 20
  • 29
89
votes
6 answers

Tuple Unpacking in Map Operations

I frequently find myself working with Lists, Seqs, and Iterators of Tuples and would like to do something like the following, val arrayOfTuples = List((1, "Two"), (3, "Four")) arrayOfTuples.map { (e1: Int, e2: String) => e1.toString + e2 } However,…
duckworthd
  • 14,679
  • 16
  • 53
  • 68
89
votes
1 answer

Python lambda does not accept tuple argument

I am running Eclipse SDK v3.6 with PyDev v2.6 plugin on two PC, with Linux and Windows. I would like to pass a tuple as an argument, like: foo = lambda (x,y): (y,x) print (foo((1,2))) This works on Linux and gives the correct result: > (2,1) On…
Dmitry
  • 1,556
  • 1
  • 14
  • 26
86
votes
1 answer

Why is starred iterable unpacking in a return statement invalid syntax without parentheses before Python 3.8?

The Python language (especially 3.x) allows very general unpacking of iterables, a simple example of which is a, *rest = 1, 2, 3 Over the years, this unpacking has been gradually generalized (see e.g. PEP 3132 and PEP 448), allowing it to be used…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
86
votes
8 answers

How are tuples unpacked in for loops?

I stumbled across the following code: for i, a in enumerate(attributes): labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W)) e = Entry(root) e.grid(column=1, row=i) entries.append(e) entries[i].insert(INSERT,"text to…
Talisin
  • 2,370
  • 3
  • 18
  • 17
84
votes
4 answers

C++17: Keep only some members when tuple unpacking

Let's imagine you need to call the following method: std::tuple foo(); In C++17, you can call the function and unpack the tuple in a single line: auto [a, b, c] = foo(); Now, how can I proceed to store only b and c and to discard…
Antoine C.
  • 3,730
  • 5
  • 32
  • 56
84
votes
1 answer

List of Tuples to DataFrame Conversion

I have a list of tuples similar to the below: [(date1, ticker1, value1),(date1, ticker1, value2),(date1, ticker1, value3)] I want to convert this to a DataFrame with index=date1, columns=ticker1, and values = values. What is the best way to do…
molivizzy
  • 857
  • 1
  • 6
  • 8
81
votes
4 answers

Tuple pairs, finding minimum using python

I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example. data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)] How may I…
Harry Lime
  • 2,167
  • 8
  • 31
  • 53
79
votes
9 answers

Using tuples in SQL "IN" clause

I have a table containing the fields group_id and group_type and I want to query the table for all the records having any tuple (group id, group type) from a list of tuples. For example, I want to be able to do something like: SELECT * FROM…
Rafid
  • 18,991
  • 23
  • 72
  • 108