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
189
votes
10 answers

Find an element in a list of tuples

I have a list 'a' a= [(1,2),(1,4),(3,5),(5,7)] I need to find all the tuples for a particular number. say for 1 it will be result = [(1,2),(1,4)] How do I do that?
Bruce
  • 33,927
  • 76
  • 174
  • 262
179
votes
6 answers

What's the difference between System.ValueTuple and System.Tuple?

I decompiled some C# 7 libraries and saw ValueTuple generics being used. What are ValueTuples and why not Tuple instead? https://learn.microsoft.com/en-gb/dotnet/api/system.tuple https://learn.microsoft.com/en-gb/dotnet/api/system.valuetuple
Steve Fan
  • 3,019
  • 3
  • 19
  • 29
179
votes
6 answers

Pair/tuple data type in Go

I need a queue of (string, int) pairs. That's easy enough: type job struct { url string depth int } queue := make(chan job) queue <- job{url, depth} are there built-in pair/tuple data types in Go? There is support for returning multiple…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
175
votes
18 answers

How to change values in a tuple?

I have a tuple called values which contains the following: ('275', '54000', '0.0', '5000.0', '0.0') I want to change the first value (i.e., 275) in this tuple but I understand that tuples are immutable so values[0] = 200 will not work. How can I…
Dawood
  • 5,106
  • 4
  • 23
  • 27
172
votes
13 answers

Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?

With the addition of the Tuple class in .net 4, I have been trying to decide if using them in my design is a bad choice or not. The way I see it, a Tuple can be a shortcut to writing a result class (I am sure there are other uses too). So…
Jason Webb
  • 7,938
  • 9
  • 40
  • 49
171
votes
5 answers

How to create a "singleton" tuple with only one element

In the below example I would expect all the elements to be tuples, why is a tuple converted to a string when it only contains a single string? >>> a = [('a'), ('b'), ('c', 'd')] >>> a ['a', 'b', ('c', 'd')] >>> >>> for elem in a: ... print…
Russell
  • 2,692
  • 5
  • 23
  • 24
169
votes
13 answers

How do I expand a tuple into variadic template function's arguments?

Consider the case of a templated function with variadic template arguments: template Tret func(const T&... t); Now, I have a tuple t of values. How do I call func() using the tuple values as arguments? I've read about…
Gustaf
164
votes
6 answers

How can I split a column of tuples in a Pandas dataframe?

I have a Pandas dataframe (this is only a little piece) >>> d1 y norm test y norm train len(y_train) len(y_test) \ 0 64.904368 116.151232 1645 549 1 70.852681 112.639876 1645 549 …
Donbeo
  • 17,067
  • 37
  • 114
  • 188
163
votes
5 answers

List of tuples to dictionary

Here's how I'm currently converting a list of tuples to dictionary in Python: l = [('a',1),('b',2)] h = {} [h.update({k:v}) for k,v in l] > [None, None] h > {'a': 1, 'b': 2} Is there a better way? It seems like there should be a one-liner to do…
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
159
votes
9 answers

Explicitly select items from a list or tuple

I have the following Python list (can also be a tuple): myList = ['foo', 'bar', 'baz', 'quux'] I can say >>> myList[0:3] ['foo', 'bar', 'baz'] >>> myList[::2] ['foo', 'baz'] >>> myList[1::2] ['bar', 'quux'] How do I explicitly pick out items whose…
Kit
  • 30,365
  • 39
  • 105
  • 149
158
votes
10 answers

What is the syntax rule for having trailing commas in tuple definitions?

In the case of a single element tuple, the trailing comma is required. a = ('foo',) What about a tuple with multiple elements? It seems that whether the trailing comma exists or not, they are both valid. Is this correct? Having a trailing comma is…
Stan
  • 37,207
  • 50
  • 124
  • 185
153
votes
5 answers

Python convert tuple to string

I have a tuple of characters like such: ('a', 'b', 'c', 'd', 'g', 'x', 'r', 'e') How do I convert it to a string so that it is like: 'abcdgxre'
intel3
  • 1,561
  • 2
  • 9
  • 4
151
votes
7 answers

How to convert list of tuples to multiple lists?

Suppose I have a list of tuples and I want to convert to multiple lists. For example, the list of tuples is [(1,2),(3,4),(5,6),] Is there any built-in function in Python that convert it to: [1,3,5],[2,4,6] This can be a simple program. But I am…
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
151
votes
4 answers

Are HLists nothing more than a convoluted way of writing tuples?

I am really interested in finding out where the differences are, and more generally, to identify canonical use cases where HLists cannot be used (or rather, don't yield any benefits over regular lists). (I am aware that there are 22 (I believe)…
Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71
148
votes
15 answers

Python element-wise tuple operations like sum

Is there anyway to get tuple operations in Python to work like this: >>> a = (1,2,3) >>> b = (3,2,1) >>> a + b (4,4,4) instead of: >>> a = (1,2,3) >>> b = (3,2,1) >>> a + b (1,2,3,3,2,1) I know it works like that because the __add__ and __mul__…
Rodrigo
  • 5,938
  • 6
  • 31
  • 39