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

Get min max values from list of tuples

I have a list of tuples, corresponding to (x,y) coordinates of some points (can be from 8 to hundreds of points): mylist = [(x0,y0), (x1,y1), ..., (xn,yn)] I want to get the min and the max values of x and y coordinates (min of all x, whatever…
rvil76
  • 159
  • 8
4
votes
2 answers

How can I join tuples to strings in a Pandas dataframe?

I am trying to join tuples in a column of my pandas dataframe and place that string in a new, separate column. For example: df = pd.DataFrame({'Number': ['001', '002', '003'], 'Tuple': [('hey', 'you'), ('you', 'can'), ('can',…
nellac77
  • 65
  • 9
4
votes
4 answers

Using tuples with .format in python

while using the .format() with text filling, I encounter this error. what I have: tuple = ('a', 'b', 'c') text = "Hi {} hello {} ola {}" #command I tried to run text.format(tuple) The output I am aiming for: Hi a hello b ola c the error I…
4
votes
2 answers

Duplicate values in Julia with Function

I need writing a function which takes as input a = [12,39,48,36] and produces as output b=[4,4,4,13,13,13,16,16,16,12,12,12] where the idea is to repeat one element three times or two times (this should be variable) and divided by 2 or 3. I tried…
4
votes
4 answers

Unique List of Tuples By First Value of Tuples

How do I make unique list of tuples by their first values in the most Pythonic way? Example: list_of_tuples = [('a', 1), ('a', 2), ('b', 3)] # Apply here magical Pythonic one liner. print(list_of_tuples) [('a', 1), ('b', 3)]
0x01h
  • 843
  • 7
  • 13
4
votes
1 answer

Sum values inside a list of tuples and order by weekday

I have a list of tuples holding ('Day of Week', n) as follows: [('Wed', 1), ('Wed', 1), ('Thu', 1), ('Thu', 0), ('Tue', 0), ('Mon', 0), ('Sun', 0), ('Sat', 0), ('Fri', 0)] I want to produce the following output, a list of tuples holding ('Day of…
4
votes
1 answer

How does this code extract the max and min from a string of integers?

This is a solution I found on codewars.com solving for this kata. def high_and_low(numbers): return " ".join(x(numbers.split(), key=int) for x in (max, min)) I do not understand how this solution works, the x function looks like a sort function,…
kuku
  • 109
  • 1
  • 6
4
votes
2 answers

Convert pandas dataframe into a list of unique tuple

What is the most efficient way to convert a pandas dataframe into a list of unique tuple? In the code below I am trying to extract a list of tuples containing all the unique PostalCode and Age. from typing import NamedTuple, Sequence, Tuple import…
Michael
  • 2,436
  • 1
  • 36
  • 57
4
votes
1 answer

ValueTuple set fields via reflection

I'm trying to set the fields of a ValueTuple at runtime using reflection and am having an issue. I suspect I'm not understanding something about how FieldInfo.SetValue works. var a = (5, 5); a.Item1 = 6; Console.WriteLine(a.Item1); // 6 var t =…
MgSam
  • 12,139
  • 19
  • 64
  • 95
4
votes
1 answer

How to unbox tuple?

I have boxed tuple: (int, string) tuple = (1, "abc"); object box = tuple; How to obtain tuple from box? What is the right syntax to cast object back to tuple? My attempt: var deconstruct = (int, string)box; is obviously wrong: Error CS1525 …
Sinatr
  • 20,892
  • 15
  • 90
  • 319
4
votes
4 answers

What's the best way to turn these nested strings in prefix notation into tuples?

I would like a general solution to transforming strings that are in prefix notation into tuples. Here are three examples below that illustrate what I hope to achieve. Each string is an element in a list (only the second example has more than one…
Steve
  • 77
  • 5
4
votes
4 answers

Problem passing Tuple to MySQLdb in python

I have some sql queries that work through python mysqldb to my mysql database, but i want to make them a little less sql-injection vulnerable so little bobby tables doesn't try to add data.. for example: ORIGINAL: (This works, so ListID etc is…
alsandair
  • 302
  • 4
  • 12
4
votes
1 answer

How to pass tuple results in one line

I have a method that returns logLevel and string, and I call it like this: Tuple levelAndMessage = SomeMethod(); logger.Log(levelAndMessage.Item1, levelAndMessage.Item2); I would like to change it to one line (to not declare a…
DisplayMyName
  • 329
  • 3
  • 15
4
votes
5 answers

Remove elements from a tuple conditional on other elements

I have the following tuple text =[('Michael', 'PERSON'), ('Jordan', 'PERSON'), ("'s", 'O'), ('legacy', 'O'), ('in', 'O'), ('the', 'O'), ('90', 'O'), ("'s", 'O'), ('shows', 'O'), ('that', 'O'), ('he', 'O'), ('was', 'O'), ('the', 'O'), ('biggest',…
Paulo Alves
  • 53
  • 1
  • 7
4
votes
1 answer

condition of tuple of integers

how do I write a concise/neat boolean condition to test if all the integers in a tuple are in a given range? Something like 0 < (1,2,3) < 50 would be perfect - of course that doesn't work because it uses lexicographical ordering, so also 0 <…
nareto
  • 185
  • 1
  • 11