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
122
votes
7 answers

Why in Python does "0, 0 == (0, 0)" equal "(0, False)"?

In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well): (0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) ==…
Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28
121
votes
3 answers

What's the meaning of "(1,) == 1," in Python?

I'm testing the tuple structure, and I found it's strange when I use the == operator like: >>> (1,) == 1, Out: (False,) When I assign these two expressions to a variable, the result is true: >>> a = (1,) >>> b = 1, >>> a==b Out: True This…
Pythoner
  • 5,265
  • 5
  • 33
  • 49
119
votes
2 answers

How to set std::tuple element by index?

One can get an element from std::tuple by index using std::get. Analogically, how to set tuple's element by index?
Behrouz.M
  • 3,445
  • 6
  • 37
  • 64
115
votes
8 answers

Python: Tuples/dictionaries as keys, select, sort

Suppose I have quantities of fruits of different colors, e.g., 24 blue bananas, 12 green apples, 0 blue strawberries and so on. I'd like to organize them in a data structure in Python that allows for easy selection and sorting. My idea was to put…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
114
votes
4 answers

Implementing comparison operators via 'tuple' and 'tie', a good idea?

(Note: tuple and tie can be taken from Boost or C++11.) When writing small structs with only two elements, I sometimes tend to choose a std::pair, as all important stuff is already done for that datatype, like operator< for strict-weak-ordering. The…
Xeo
  • 129,499
  • 52
  • 291
  • 397
114
votes
2 answers

What is the pythonic way to unpack tuples as function arguments?

This is ugly. What's a more Pythonic way to do it? import datetime t= (2010, 10, 2, 11, 4, 0, 2, 41, 0) dt = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], t[6])
user132262
  • 1,837
  • 4
  • 16
  • 17
113
votes
15 answers

Assign multiple new variables on LHS in a single line

I want to assign multiple variables in a single line in R. Is it possible to do something like this? values # initialize some vector of values (a, b) = values[c(2,4)] # assign a and b to values at 2 and 4 indices of 'values' Typically I want to…
user236215
  • 7,278
  • 23
  • 59
  • 87
110
votes
6 answers

Returning two values, Tuple vs 'out' vs 'struct'

Consider a function which returns two values. We can write: // Using out: string MyFunction(string input, out int count) // Using Tuple class: Tuple MyFunction(string input) // Using struct: MyStruct MyFunction(string input) Which…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
110
votes
16 answers

Subtracting 2 lists in Python

Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like [2,2,2] - [1,1,1] = [1,1,1] Should I use tuples? If none of them defines these operands on these types, can I define it…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
110
votes
11 answers

Transform "list of tuples" into a flat list or a matrix

With Sqlite, a select .. from command returns the results output, which prints: >>print output [(12.2817, 12.2817), (0, 0), (8.52, 8.52)] It seems to be a list of tuples. I would like to either convert output to a simple list: [12.2817, 12.2817, 0,…
garth
  • 1,149
  • 2
  • 10
  • 5
108
votes
13 answers

In what case would I use a tuple as a dictionary key?

I was studying the difference between lists and tuples (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable. A sentence in the article got me: Only immutable…
Escualo
  • 40,844
  • 23
  • 87
  • 135
103
votes
8 answers

How to search a list of tuples in Python

So I have a list of tuples such as this: [(1,"juca"),(22,"james"),(53,"xuxa"),(44,"delicia")] I want this list for a tuple whose number value is equal to something. So that if I do search(53) it will return the index value of 2 Is there an easy way…
hdx
  • 4,198
  • 7
  • 26
  • 33
101
votes
5 answers

scala tuple unpacking

I know this question has come up many times in different ways. But it is still not clear to me. Is there a way to achieve the following. def foo(a:Int, b:Int) = {} foo(a,b) //right way to invoke foo foo(getParams) // is there a way to get this…
scout
  • 2,336
  • 4
  • 21
  • 22
100
votes
13 answers

Pretty-print std::tuple

This is a follow-up to my previous question on pretty-printing STL containers, for which we managed to develop a very elegant and fully general solution. In this next step, I would like to include pretty-printing for std::tuple, using…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
98
votes
11 answers

When to use: Tuple vs Class in C# 7.0

Before Tuples, I used to create a class and its variables, then create object from this class and make that object the return type for some functions. Now, with tuples, I can do the same thing, and in C# 7.0 we can assign understandable names for…
Madonna Remon
  • 1,149
  • 1
  • 7
  • 12