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
4
votes
1 answer

Tuple wrapper that works with get, tie, and other tuple operations

I have written a fancy "zip iterator" that already fulfils many roles (can be used in for_each, copy loops, container iterator range constructors etc...). Under all the template code to work around the pairs/tuples involved, it comes down to the…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
4
votes
1 answer

List> as DataSource for GridView

I'd like to add a List> as DataSource for my GridView. "Then do it!" Yeah, that's not really the problem, the problem is accessing the values inside the GridView. Here's my Code: List> userGroups =…
Dennis Röttger
  • 1,975
  • 6
  • 32
  • 51
4
votes
3 answers

How to implement uncurry point-free in Haskell without app?

I have been wondering how different standard Haskell functions could be implemented point-free. Currently, I am interested in uncurry and I feel this one is quite non-trivial. The main problem is that we are unable (or as it seems to me) to group…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
4
votes
2 answers

Haskell Tuple destructuring on infinite lists behaves differently when destructuring the Tuple as an argument than when destructuring using let

When trying to implement dropWhile using foldr the first algorithm that I came up with was this dropWhile' :: (a -> Bool) -> [a] -> [a] dropWhile' pred = fst . foldr (\cur (acc, xs) -> if pred cur then (acc, cur:xs) else (cur:xs,…
Prophet
  • 186
  • 8
4
votes
3 answers

Pandas splitting Columns and creating Columns of tuples

I have a dataframe which looks as follows: # df colA colB colC rqp 129 a pot 217;345 u ghay 716 b rbba 217;345 d tary 612;811;760 a kals 716 t The ColB…
Stan
  • 786
  • 1
  • 9
  • 25
4
votes
2 answers

Is there a way to search just for the first coordinate in a list of tuples?

Assuming I have a list of tuples like the following: a = [('a','b'), ('c','d'), ('e','f')] If I were to execute this line 'a' in a I would get False. Is there a way to tell python "search the just for the first argument and accept whatever in the…
Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34
4
votes
2 answers

Tuple templates in GCC

I first started C++ with Microsoft VC++ in VS2010. I recently found some work, but I've been using RHEL 5 with GCC. My code is mostly native C++, but I've noticed one thing... GCC doesn't appear to recognize the header file, or the tuple…
sj755
  • 3,944
  • 14
  • 59
  • 79
4
votes
2 answers

What is the type constructor for list of tuples?

The question What is the type constructor which generates a concrete type like [(Int,String)]? The origin of the question I think this really sounds either like a stupid question or a meaningless riddle. I hope the following convinces you that I'm…
Enlico
  • 23,259
  • 6
  • 48
  • 102
4
votes
1 answer

Namedtuple not defined on windows interpreter

I'm trying to follow the usage example of namedtuple( ) from the documentation, but I keep getting namedtuple is not defined.
Paw in Data
  • 1,262
  • 2
  • 14
  • 32
4
votes
1 answer

Replace the type of the last parameter in a function typescript

I want to replace the type of the last parameter in a function whilst preserving the names of all function parameters. In my case the last argument is also optional. eg: type OrigArg = { arg: number }; type ReplacedArg = { arg: string }; type…
yahgwai
  • 43
  • 3
4
votes
0 answers

Referring to types of tuple fields

Is there any way to refer to the types of a tuple's fields without recapitulation? Something along the lines of this: pub struct Foo(i16, u64); impl Foo { pub fn get_bar(&self) -> Self::0 { self.0 } pub fn get_baz(&self) -> Self::1 { self.1…
Reid Rankin
  • 1,078
  • 8
  • 26
4
votes
5 answers

How to add (not append) a number to an existing value in a tuple?

How could I do something like this without getting an error: x,y = 0,0 x2,y2 = 1,1 lst = [(x,y), (x2,y2)] lst[0][1] += 32 When I do that I get a TypeError, and the try method doesn't do what I want to do.
Stqrosta
  • 352
  • 2
  • 10
4
votes
1 answer

Rewrite my simple Python code in a better mode

I have a function def max_f(tup, val): max = tup[0](val) out = tup[0] for funz in tup: new = funz(val) if new > max: max = new out = funz return out and I would like to write it in a better…
Alberto
  • 83
  • 2
  • 7
4
votes
2 answers

Append unequal length of values from a list to a dictionary

I have a dictionary, the keys of which are: {'A', 'B', 'C', 'D'} and some examples of list values: 1- [(33.0, 134), (54.0, 113), (21.0, 120)] 2- [(25.0, 153)] 3- [()] I want to apply the list values to the dictionary keys in order. The expected…
Scrappy Coco
  • 96
  • 3
  • 13
4
votes
3 answers

What makes a python tuple?

The following code does not print what I would expect: #!/usr/bin/env python print type(1,) a = 1, print type(a) Here is the output: I understand that the comma makes a into a tuple. But if that is the case how come…
Ivan Novick
  • 745
  • 2
  • 8
  • 12