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

C#: How do i get 2 lists into one 2-tuple list in

I have 2 Lists. First one is Type string. The second is type object. Now I want to get both lists into a Tuple. like this var List = new(string list1, object list2)[] How do I do this? I had to create 2 seperate lists, because I…
Max Rowe
  • 81
  • 2
  • 7
4
votes
6 answers

Python, tuple arguments playing nicely with others

For example: mytuple = ("Hello","World") def printstuff(one,two,three): print one,two,three printstuff(mytuple," How are you") This naturally crashes out with a TypeError because I'm only giving it two arguments when it expects three. Is…
Bolster
  • 7,460
  • 13
  • 61
  • 96
4
votes
2 answers

Iterating over tuple in C++17/20

Does anyone know of a good, clean way to iterate over a tuple in C++17 / 20? Let's say we have a bit of code like this: class Test { public: Test( int x ) : x_(x) {}; void Go() const { std::cout << "Hi!" << x_ << "\n" ; } …
JoshK
  • 337
  • 4
  • 16
4
votes
6 answers

Creating a random name generator. How do I accomplish this?

I'm trying to grab a single item from each of the Lists here, and combine them to make a unique name. This is just for kicks. :) Here are the lists: List FirstNames = new List() { "Sergio", "Daniel", "Carolina", …
delete
4
votes
1 answer

Storing 2 variables at once from a tuple function

I have a tuple function that returns a tuple of the form Is there a way to store 2 values at once without creating another tuple. I know we can do n,score=tuplefunct(abc); in python. But if I want to store both return values in c++…
Jon Bovi
  • 53
  • 9
4
votes
3 answers

How to arrange a list of tuples so that the tuple associated with the highest value compared to other tuple is removed and returns the maximum one

val = [(200, []), (300, [500, 200]), (400, [100, 200, 300]), (400, [])] largest_val_arrangement(val) [(200, []), (300, [500, 200]), (400, [100, 200, 300])] so now (400, []) is poped out because (400, [100, 200, 300]) has more elements than it.
4
votes
1 answer

Read nested array and tuples in JSON in Swift

I have some data passed via JSON which is an array of tuples containing arrays, and I can't extract the data out of it. I've tried to cast to different types, but the only thing that works is [Any] and I can't break that down further. The JSON: { …
thebucc
  • 207
  • 1
  • 10
4
votes
3 answers

How to modify each element of a tuple in a list of tuples

I have a list of tuples: my_list = [(0.12497007846832275, 0.37186527252197266, 0.9681450128555298, 0.5542989373207092), (0.18757864832878113, 0.6171307563781738, 0.8482183218002319, 0.8088157176971436), (0.06923380494117737,…
S Andrew
  • 5,592
  • 27
  • 115
  • 237
4
votes
3 answers

Formatting list of tuples to decimals and adding them to a file

I have a list of tuples as coordinates (it has to be this way) points = [(x1, y1), (x2, y2), ...] to draw a polygon in matplotlib. To get these coordinates I first created an empty list points = [] and then wrote a function to calculate each point…
spfortray
  • 91
  • 1
  • 7
4
votes
4 answers

Create column in pandas dataframe based on condition

I have a dataframe, want to create third column say col3 based on the condition if col2 value is present in col1 then 'Yes' else 'No' data = [[[('330420', 0.9322496056556702), ('76546', 0.9322003126144409)],76546],[[('330420', 0.9322496056556702),…
user15051990
  • 1,835
  • 2
  • 28
  • 42
4
votes
2 answers

Pandas apply tuple unpack function on multiple columns

Given a function that takes multiple arguments and returns multiple values as so: def tuple_unpack(value, another_value): ''' does some interesting stuff ... ''' return value, another_value Is there a way to apply such function to a pandas…
callmeGuy
  • 944
  • 2
  • 11
  • 28
4
votes
3 answers

Name tuple variables in Scala

Right now I have a def that returns a Tuple2[String, Int], or (String, Int). def makeTuple(name: String, age: Int) : (String, Int) = { (name, age) } val nameAge = makeTuple("test", 12) println(nameAge._1) // prints test println(nameAge._2) //…
Mr.Turtle
  • 2,950
  • 6
  • 28
  • 46
4
votes
1 answer

Flattening arbitrarily nested tuples containing case class elements using Shapeless

I used the following example: https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/flatten.scala to flatten tuples. However I have now realized that case classes are also flattened. Is their any way I can…
user2051561
  • 838
  • 1
  • 7
  • 21
4
votes
4 answers

Why can you assign new value to tuples with concatenation?

Take the following code for example: t=(1,2,3) t+=(4,) print(t) The printed value is (1,2,3,4).Didn't the value of tuple t just got changed which is similar to an append/extend method for list objects?
Oliver H
  • 65
  • 1
  • 7
4
votes
2 answers

How can I treat a type that is essentially a tuple as a tuple in F#

Ok, so let's say I have a type defined like so: type Foo = | Bar of (SomeType * SomeType * SomeType * SomeType) | ...(other defs) so I have a Bar, that is basically a tuple of 4 SomeTypes. I want to access individual members of the tuple.…
jeff
  • 968
  • 1
  • 6
  • 13
1 2 3
99
100