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
1192
votes
13 answers

What are "named tuples" in Python?

What are named tuples and how do I use them? When should I use named tuples instead of normal tuples, or vice versa? Are there "named lists" too? (i.e. mutable named tuples) For the last question specifically, see also Existence of mutable named…
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
1145
votes
22 answers

What's the difference between lists and tuples?

What's the difference between tuples/lists and what are their advantages/disadvantages?
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83
892
votes
11 answers

How to sort a list/tuple of lists/tuples by the element at a given index

I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5,6], [7,8,9]] data = [(1,2,3), (4,5,6), (7,8,9)] And I want to sort by the 2nd element in the subset. Meaning, sorting by 2,5,8 where 2 is from (1,2,3),…
Stan
  • 37,207
  • 50
  • 124
  • 185
713
votes
37 answers

What is the equivalent of the C++ Pair in Java?

Is there a good reason why there is no Pair in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own. It seems that 1.6 is providing something similar (AbstractMap.SimpleEntry), but this looks…
David Segonds
  • 83,345
  • 10
  • 45
  • 66
678
votes
8 answers

Convert list to tuple in Python

I'm trying to convert a list to a tuple. Most solutions on Google offer the following code: l = [4,5,6] tuple(l) However, the code results in an error message when I run it: TypeError: 'tuple' object is not callable How can I fix this problem?
LynnH
  • 7,216
  • 3
  • 15
  • 13
552
votes
5 answers

Expanding tuples into arguments

Suppose I have a function like: def myfun(a, b, c): return (a * 2, b + c, c + b) Given a tuple some_tuple = (1, "foo", "bar"), how would I use some_tuple to call myfun? This should output the result (2, "foobar", "barfoo"). I know could define…
AkiRoss
  • 11,745
  • 6
  • 59
  • 86
541
votes
9 answers

Sort a list of tuples by 2nd item (integer value)

I have a list of tuples that looks something like this: [('abc', 121),('abc', 231),('abc', 148), ('abc',221)] I want to sort this list in ascending order by the integer value inside the tuples. Is it possible?
Amyth
  • 32,527
  • 26
  • 93
  • 135
538
votes
7 answers

List vs tuple, when to use each?

In Python, when should you use lists and when tuples? Sometimes you don't have a choice, for example if you have "hello %s you are %s years old" % x then x must be a tuple. But if I am the one who designs the API and gets to choose the data types,…
flybywire
  • 261,858
  • 191
  • 397
  • 503
512
votes
13 answers

Why is there no tuple comprehension in Python?

As we all know, there's list comprehension, like [i for i in [1, 2, 3, 4]] and there is dictionary comprehension, like {i:j for i, j in {1: 'a', 2: 'b'}.items()} but (i for i in (1, 2, 3)) will end up in a generator, not a tuple comprehension.…
396
votes
8 answers

How to easily initialize a list of Tuples?

I love tuples. They allow you to quickly group relevant information together without having to write a struct or class for it. This is very useful while refactoring very localized code. Initializing a list of them however seems a bit redundant. var…
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
390
votes
12 answers

Ignore python multiple return value

Say I have a Python function that returns multiple values in a tuple: def func(): return 1, 2 Is there a nice way to ignore one of the results rather than just assigning to a temporary variable? Say if I was only interested in the first value,…
Mat
  • 82,161
  • 34
  • 89
  • 109
375
votes
9 answers

Add variables to tuple

I am creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. I am taking information from the user and store it in variables. Can I add these variables into a…
amit
  • 10,133
  • 22
  • 72
  • 121
364
votes
10 answers

How to merge lists into a list of tuples?

What is the Pythonic approach to achieve the following? # Original lists: list_a = [1, 2, 3, 4] list_b = [5, 6, 7, 8] # List of tuples from 'list_a' and 'list_b': list_c = [(1,5), (2,6), (3,7), (4,8)] Each member of list_c is a tuple, whose…
rubayeet
  • 9,269
  • 8
  • 46
  • 55
359
votes
14 answers

Using Pairs or 2-tuples in Java

My Hashtable in Java would benefit from a value having a tuple structure. What data structure can I use in Java to do that? Hashtable,Set>> table = ...
syker
  • 10,912
  • 16
  • 56
  • 68
319
votes
19 answers

Better naming in Tuple classes than "Item1", "Item2"

Is there a way to use a Tuple class, but supply the names of the items in it? For example: public Tuple GetOrderRelatedIds() That returns the ids for OrderGroupId, OrderTypeId, OrderSubTypeId and OrderRequirementId. It would be…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
1
2 3
99 100