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
3 answers

How to "explicitly specify the categories order by passing in a categories argument" when using tuples as index keys in pandas?

I've been trying to figure out how to make these tuples index keys in pandas but I'm getting an error. How can I use the suggestion from the error with pd.Categorical below to fix this error? I am aware that I can convert to a string but I am…
O.rka
  • 29,847
  • 68
  • 194
  • 309
4
votes
1 answer

A function of tuple of lists

I am sorry if this question is very trivial, but I have struggled to find an answer to it for quite a time, that is why I decided to post it here. I want to build a function which takes a list of binary tuples: [(Integer, Integer)] and outputs the…
4
votes
1 answer

Select all possible tuples from a vector in R

I'm trying to write a program in R that when, given a vector, will return all possible tuples of elements from that vector. For example: tuples(c('a','b','c')) = c('a','b','c'); c('a','b'); c('a','c'), c('b','c'); c('a'); c('b'); c('c') I think it…
Zach
  • 29,791
  • 35
  • 142
  • 201
4
votes
3 answers

Dictionaries vs NamedTuples

Aside that Dictionaries are mutable and NamedTuple not, that NamedTuple can be retrieved by position and a bit of different notation, are there other significant differences between Dictionaries and NamedTuples in Julia ? When to use one or the…
Antonello
  • 6,092
  • 3
  • 31
  • 56
4
votes
2 answers

Are Python3.5 tuple comprehension really this limited?

I've been loving the tuple comprehensions added to Python3.5: In [128]: *(x for x in range(5)), Out[128]: (0, 1, 2, 3, 4) However, when I try to return a tuple comprehension directly I get an error: In [133]: def testFunc(): ...: return…
ojunk
  • 879
  • 8
  • 21
4
votes
2 answers

What's the difference between System.Tuple and (,)?

Background: I am using two methods from different libraries; one uses System.Tuple and the other uses (double,double) for arguments. I am finding myself unable to utilize both methods without doing extra work to convert a System.Tuple…
pavuxun
  • 411
  • 2
  • 15
4
votes
1 answer

A non-homogenous tuple of fixed size

Is there any way to define a function accepting only tuples of fixed sizes, but different data types in them? We can use something like this f(x::NTuple{N}...) where {N} = ... to enforce tuples of the same size (but with content of the same type),…
Šimon Mandlík
  • 303
  • 1
  • 6
4
votes
2 answers

Strange Scala 'Type mismatch' error for tuples

I have a function map which takes a Parser and a function defined as follows: def map[T1, T2](parser: Parser[T1], func: T1 => T2): Parser[T2] I've created a Parser object of type [(Char, Char)] and a function (Char, Char) => String. val…
Durga Swaroop
  • 563
  • 1
  • 6
  • 25
4
votes
2 answers

trying to see if a variable is included in a list in django

I am trying to find if a selected country is in a list of tuples by using the following command in my view: if second_tier.profile.country in COUNTRIES: if I print second_tier.profile.country I have CA and when I print COUNTRIES I get: (('GB',…
RC-
  • 163
  • 10
4
votes
2 answers

Python tuples: compare and merge without for loops

I have two lists of over 100,000 tuples in each. The first tuple list has two strings in it, the latter has five. Each tuple within the first list has a tuple with a common value in the other list. For example tuple1 = [('a','1'), ('b','2'),…
JRR
  • 578
  • 5
  • 21
4
votes
1 answer

MoreLinq ExceptBy with only one matching property

I have 2 different items that I'm trying to perform the except by method using the more linq library. The only thing that is common between both items is a single string called Symbol. Item 1 is a list of tuples and Item 2 is a database table that I…
DarthVegan
  • 1,719
  • 7
  • 25
  • 42
4
votes
4 answers

create a tuple with map, with conditional evenorodd list

Here is my code : evenorodd=[1,2,3] list1=['a','b','c'] list2=['A','B','C'] res = tuple(map(lambda x: True if x % 2 != 0 else False, evenorodd)) print(res) the output: (False, True, False, True) I want this : element of list1 if x%2!=0 (if…
bryan tetris
  • 123
  • 1
  • 3
  • 7
4
votes
1 answer

Converting list to tuple in Python

>>> list=['a','b'] >>> tuple=tuple(list) >>> list.append('a') >>> print(tuple) ('a', 'b') >>> another_tuple=tuple(list) Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object is not callable Why cannot I…
rooni
  • 1,036
  • 3
  • 17
  • 33
4
votes
2 answers

returning date with traffic intensity from tuples in a list

I'm trying to return all the dates that match with the traffic intensity in the function below but am getting all the elements of only the first tuple as a list instead of all tuples that match. def traffic_intensity(count): """Returns string…
user9644895
4
votes
2 answers

Python - How to Create Dictionary from CSV data file using column headings

I am trying to create a function that accepts the name of a .csv data file and a list of strings representing column headings in that file and return a dict object with each key being a column heading and the corresponding value being a numpy array…
user8679420
1 2 3
99
100