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
148
votes
9 answers

Why do we need tuples in Python (or any immutable data type)?

I've read several python tutorials (Dive Into Python, for one), and the language reference on Python.org - I don't see why the language needs tuples. Tuples have no methods compared to a list or set, and if I must convert a tuple to a set or list to…
pyNewGuy
  • 1,489
  • 2
  • 10
  • 3
147
votes
8 answers

How to extract the n-th elements from a list of tuples

I'm trying to obtain the n-th elements from a list of tuples. I have something like: elements = [(1,1,1),(2,3,7),(3,5,10)] I wish to extract only the second elements of each tuple into a list: seconds = [1, 3, 5] I know that it could be done with…
pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
140
votes
7 answers

Python : List of dict, if exists increment a dict value, if not append a new dict

I would like do something like that. list_of_urls = ['http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.cn/', 'http://www.google.com/', 'http://www.google.fr/', 'http://www.google.fr/', …
Natim
  • 17,274
  • 23
  • 92
  • 150
139
votes
12 answers

JavaScript variable assignments from tuples

In other languages like Python 2 and Python 3, you can define and assign values to a tuple variable, and retrieve their values like this: tuple = ("Bob", 24) name, age = tuple print(name) #name evaluates to Bob print(age) #age…
Karl
  • 6,035
  • 5
  • 30
  • 39
138
votes
11 answers

Why can't I use a list as a dict key in python? Exactly what can and cannot be used, and why?

I found that the following are all valid: >>> d = {} >>> d[None] = 'foo' >>> d[(1, 3)] = 'baz' Even a module can be used as a dict key: >>> import sys >>> d[sys] = 'bar' However, a list cannot, and neither can a tuple that contains a list: >>>…
wim
  • 338,267
  • 99
  • 616
  • 750
138
votes
7 answers

Create a Tuple in a Linq Select

I'm working with C# and .NET Framework 4.5.1 retrieving data from a SQL Server database with Entity Framework 6.1.3. I have this: codes = codesRepo.SearchFor(predicate) .Select(c => new Tuple(c.Id, c.Flag)) .ToList(); And…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
135
votes
3 answers

How to find the maximum value in a list of tuples?

I have a list with ~10^6 tuples in it like this: [(101, 153), (255, 827), (361, 961), ...] ^ ^ X Y I want to find the maximum value of the Ys in this list, but also want to know the X that it is bound to. How do I do this?
Berk Özbalcı
  • 3,016
  • 3
  • 21
  • 27
131
votes
6 answers

Difference between std::pair and std::tuple with only two members?

Is there a difference between an std::pair and an std::tuple with only two members? (Besides the obvious that std::pair requires two and only two members and tuple may have more or less...)
Casey
  • 10,297
  • 11
  • 59
  • 88
130
votes
5 answers

What and When to use Tuple?

May someone please explain what a Tuple is and how to use it in a Real World Scenario. I would like to find out how this can enrich my coding experience?
Donald N. Mafa
  • 5,131
  • 10
  • 39
  • 56
128
votes
9 answers

Tuples (or arrays) as Dictionary keys in C#

I am trying to make a Dictionary lookup table in C#. I need to resolve a 3-tuple of values to one string. I tried using arrays as keys, but that did not work, and I don't know what else to do. At this point I am considering making a Dictionary of…
AlexH
  • 2,827
  • 7
  • 31
  • 30
128
votes
12 answers

Why is the use of tuples in C++ not more common?

Why does nobody seem to use tuples in C++, either the Boost Tuple Library or the standard library for TR1? I have read a lot of C++ code, and very rarely do I see the use of tuples, but I often see lots of places where tuples would solve many…
Zifre
  • 26,504
  • 11
  • 85
  • 105
127
votes
4 answers

Why do tuples take less space in memory than lists?

A tuple takes less memory space in Python: >>> a = (1,2,3) >>> a.__sizeof__() 48 whereas lists takes more memory space: >>> b = [1,2,3] >>> b.__sizeof__() 64 What happens internally on the Python memory management?
JON
  • 1,668
  • 2
  • 15
  • 18
127
votes
7 answers

Does Python have an immutable list?

Does python have immutable lists? Suppose I wish to have the functionality of an ordered collection of elements, but which I want to guarantee will not change, how can this be implemented? Lists are ordered but they can be mutated.
cammil
  • 9,499
  • 15
  • 55
  • 89
124
votes
13 answers

C++ Tuple vs Struct

Is there is any difference between using a std::tuple and a data-only struct? typedef std::tuple foo_t; struct bar_t { int id; double value; bool dirty; } From what I have found online, I found that there are two…
Alex Koay
  • 2,915
  • 4
  • 18
  • 16
123
votes
3 answers

Sort tuples based on second parameter

I have a list of tuples that look something like this: ("Person 1",10) ("Person 2",8) ("Person 3",12) ("Person 4",20) What I want produced, is the list sorted in ascending order, by the second value of the tuple. So L[0] should be ("Person 2", 8)…
user974703
  • 1,653
  • 4
  • 20
  • 27