Questions tagged [tuples]

In programming, tuples are simple *product types*, representing ordered collections of types.

tuples
Filter by
Sorted by
Tagged with
55 votes
5 answers
63k views

Tuple.Create() vs new Tuple

Consider the following expressions: new Tuple<int,int>(1,2); Tuple.Create(1,2); Is there any difference between these two methods of Tuple creation? From my reading it seems to be more a ...
chris's user avatar
  • 4,948
54 votes
6 answers
31k views

List unhashable, but tuple hashable?

In How to hash lists? I was told that I should convert to a tuple first, e.g. [1,2,3,4,5] to (1,2,3,4,5). So the first cannot be hashed, but the second can. Why*? *I am not really looking for a ...
gsamaras's user avatar
  • 72.8k
54 votes
2 answers
27k views

Is it possible to have tuple assignment to variables in Scala? [duplicate]

Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable assignment to tuples via: val (a, b) = (1, 2) But a similar syntax for assignment to ...
Heinrich Schmetterling's user avatar
53 votes
7 answers
43k views

Template tuple - calling a function on each element

My question is in the code: template<typename... Ts> struct TupleOfVectors { std::tuple<std::vector<Ts>...> tuple; void do_something_to_each_vec() { //Question: I want to ...
7cows's user avatar
  • 5,014
52 votes
2 answers
19k views

C++11 use-case for piecewise_construct of pair and tuple?

In N3059 I found the description of piecewise construction of pairs (and tuples) (and it is in the new Standard). But I can not see when I should use it. I found discussions about emplace and non-...
towi's user avatar
  • 21.9k
51 votes
2 answers
51k views

How to return named tuples in C#?

I have a property that returns two items of type DateTime. When returning these values I have to reference them as Item1 and Item2. How do I return with custom names e.g. filter?.DateRanges.From ...
Karim Ali's user avatar
  • 523
50 votes
4 answers
22k views

Accessing a specific member in a F# tuple

In F# code I have a tuple: let myWife=("Tijana",32) I want to access each member of the tuple separately. For instance this what I want to achieve by I can't Console.WriteLine("My wife is {0} and ...
Nikola Stjelja's user avatar
50 votes
4 answers
26k views

Set (Collection) - Insert multiple elements

Set is an unordered collection of unique elements. Almost similar to array. I want to add/insert multiple elements in a Set of String. But there is only single method provided that can insert only ...
Krunal's user avatar
  • 78.6k
50 votes
1 answer
4k views

Why does '() is ()' return True when '[] is []' and '{} is {}' return False?

From what I've been aware of, using [], {} or () to instantiate objects returns a new instance of list, dict or tuple respectively; a new instance object with a new identity. This was pretty clear to ...
Dimitris Fasarakis Hilliard's user avatar
49 votes
2 answers
50k views

Interface for associative object array in TypeScript

I have an object like so: var obj = { key1: "apple", key2: true, key3: 123, . . . key{n}: ... } So obj can contain any number of named keys, but the values must all be ...
prmph's user avatar
  • 7,946
49 votes
1 answer
24k views

C#7 tuple & async

Old format: private async Task<Tuple<SomeArray[], AnotherArray[], decimal>> GetInvoiceDetailAsync(InvoiceHead invoiceHead) { ... } How can you do that in C#7 with new tuples ...
Oszkar's user avatar
  • 1,653
48 votes
9 answers
36k views

What are good use-cases for tuples in C++11?

What are good use-cases for using tuples in C++11? For example, I have a function that defines a local struct as follows: template<typename T, typename CmpF, typename LessF> void mwquicksort(T ...
zvrba's user avatar
  • 24.4k
48 votes
2 answers
109k views

Save list of ordered tuples as CSV [duplicate]

I have a list of tuples ordered by value. They are in the form (name,count) where count is number of occurrences for each unique name. I would like to take this list and transform it into CSV where ...
Edmon's user avatar
  • 4,822
48 votes
9 answers
31k views

How to loop over struct properties in Swift?

Is it possible to iterate over properties of a struct in Swift? I need to register cells-reuse identifiers in a view controller that makes use of many different cell types (cells are organized in ...
blackjacx's user avatar
  • 9,757
48 votes
3 answers
77k views

array.shape() giving error tuple not callable

I have a 2D numpy array called results, which contains its own array of data, and I want to go into it and use each list: for r in results: print "r:" print r y_pred = np.array(r)...
user961627's user avatar
  • 12.6k
47 votes
6 answers
25k views

std::make_tuple doesn't make references

I've been experimenting with std::tuple in combination with references: #include <iostream> #include <tuple> int main() { int a,b; std::tuple<int&,int&> test(a,b); ...
Flexo's user avatar
  • 88k
47 votes
7 answers
92k views

Accessing a Specific Element in a Tuple

Haskell-newbie reporting in. Question is as follows: In Haskell, we have fst and snd that return the first and the second elements of a 2-tuple. Why don't we have an easy way of accessing the i-th ...
user avatar
47 votes
8 answers
30k views

C++11 Tagged Tuple

C++11 tuples are nice, but they have two huge disadvantages to me, accessing members by index is unreadable difficult to maintain (if I add an element in the middle of the tuple, I'm screwed) In ...
user avatar
46 votes
7 answers
74k views

How do I sum the first value in each tuple in a list of tuples in Python?

I have a list of tuples (always pairs) like this: [(0, 1), (2, 3), (5, 7), (2, 1)] I'd like to find the sum of the first items in each pair, i.e.: 0 + 2 + 5 + 2 How can I do this in Python? At the ...
Ben's user avatar
  • 67.8k
46 votes
10 answers
235k views

pop/remove items out of a python tuple

I am not sure if I can make myself clear but will try. I have a tuple in python which I go through as follows (see code below). While going through it, I maintain a counter (let's call it 'n') and '...
Steve Grafton's user avatar
46 votes
2 answers
36k views

Are there any methods included in Scala to convert tuples to lists?

I have a Tuple2 of List[List[String]] and I'd like to be able to convert the tuple to a list so that I can then use List.transpose(). Is there any way to do this? Also, I know it's a Pair, though I'm ...
pr1001's user avatar
  • 21.9k
46 votes
1 answer
14k views

Python: Unpacking an inner nested tuple/list while still getting its index number

I am familiar with using enumerate(): >>> seq_flat = ('A', 'B', 'C') >>> for num, entry in enumerate(seq_flat): print num, entry 0 A 1 B 2 C I want to be able to do the ...
Kit's user avatar
  • 31k
45 votes
8 answers
62k views

How to name tuple properties? [duplicate]

How and "could be" organized return from the method which returns tuple type with the name of parameters, as an example private static Tuple<string, string> methodTuple() { return new {Name ...
AleksP's user avatar
  • 1,326
45 votes
5 answers
23k views

Anonymous type and tuple

What is the difference between anonymous type and tuple?
Amutha's user avatar
  • 541
45 votes
10 answers
13k views

Why use tuples instead of objects?

The codebase where I work has an object called Pair where A and B are the types of the first and second values in the Pair. I find this object to be offensive, because it gets used instead of an ...
Mr. Shiny and New 安宇's user avatar
45 votes
9 answers
12k views

Boost::Tuples vs Structs for return values

I'm trying to get my head around tuples (thanks @litb), and the common suggestion for their use is for functions returning > 1 value. This is something that I'd normally use a struct for , and I can'...
Roddy's user avatar
  • 67.4k
45 votes
1 answer
7k views

Haskell tuple constructor (GHC) and the separation between a language and its implementation

Haskell blew my mind yet again when I realised that (x,y) Is just syntactic sugar for (,) x y Naturally I wanted to extend this to larger tuples. But (,) x ((,) y z) Gave me (x,(y,z)) Which ...
TheIronKnuckle's user avatar
44 votes
5 answers
100k views

Tuple value by key

Is it possible to get Value out of tuple: TUPLE = ( ('P', 'Shtg1'), ('R', u'Shtg2'), ('D', 'Shtg3'), ) by calling STR key like P Python says that only int can be used for this type of '...
Mission's user avatar
  • 1,297
44 votes
6 answers
28k views

How is std::tuple implemented?

I'd like to know how are tuple implemented in standard library for C++0x. I tried to read description in libstdc++ manual and then read template listing, but it's really hard to understand how it ...
Goofy's user avatar
  • 5,277
44 votes
6 answers
180k views

How can I access each element of a pair in a pair list?

I have a list called pairs. pairs = [("a", 1), ("b", 2), ("c", 3)] And I can access elements as: for x in pairs: print x which gives output like: ('a', 1) ('b', 2) ('c', 3) But I want to ...
impossible's user avatar
  • 2,440
44 votes
3 answers
48k views

Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like --data (1,2,3,4) I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)'. I tried by ...
user1423015's user avatar
44 votes
6 answers
37k views

Why does adding a trailing comma after an expression create a tuple?

Why does adding a trailing comma after an expression create a tuple with the expression's value? E.g. in this code: >>> abc = 'mystring', >>> print(abc) ('mystring',) Why is the ...
Avadhesh's user avatar
  • 4,627
43 votes
5 answers
25k views

Converting a list of tuples into a dict

I have a list of tuples like this: [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] I want to iterate through this keying by the first item, so, for example, I could print something ...
Dan's user avatar
  • 34.7k
43 votes
3 answers
129k views

python: create list of tuples from lists [duplicate]

I have two lists: x = ['1', '2', '3'] y = ['a', 'b', 'c'] and I need to create a list of tuples from these lists, as follows: z = [('1','a'), ('2','b'), ('3','c')] I tried doing it like this: z = ...
amyassin's user avatar
  • 2,883
43 votes
2 answers
49k views

Scala return type for tuple-functions

I want to make a scala function which returns a scala tuple. I can do a function like this: def foo = (1,"hello","world") and this will work fine, but now I want to tell the compiler what I expect ...
Felix's user avatar
  • 8,435
43 votes
3 answers
11k views

Why does the 2-tuple Functor instance only apply the function to the second element?

import Control.Applicative main = print $ fmap (*2) (1,2) produces (1,4). I would expect it it to produce (2,4) but instead the function is applied only to the second element of the tuple. Update I'...
Peter Hall's user avatar
  • 56.4k
43 votes
3 answers
18k views

Use functional combinators on Scala Tuples?

'map' preserves the number of elements, so using it on a Tuple seems sensible. My attempts so far: scala> (3,4).map(_*2) error: value map is not a member of (Int, Int) (3,4).map(_*2) ...
Eldritch Conundrum's user avatar
43 votes
3 answers
6k views

Why is there no piecewise tuple construction?

The standard templates std::pair and std::array are special cases of std::tuple, and it stands to reason that they should have a very similar set of capabilities. However, uniquely among the three, ...
Kerrek SB's user avatar
  • 470k
42 votes
2 answers
57k views

Python list comprehension, unpacking and multiple operations

I want to unpack the tuples I create by doing the following so he the result is just one simple list. I can get the desired result in 2-3 lines but surely there is a oneliner list.comp? >>> x ...
arynaq's user avatar
  • 6,780
42 votes
4 answers
50k views

python - can lambda have more than one return

I know lambda doesn't have a return expression. Normally def one_return(a): #logic is here c = a + 1 return c can be written: lambda a : a + 1 How about write this one in a lambda ...
Shengjie's user avatar
  • 12.5k
41 votes
3 answers
88k views

converting string to tuple

I need to write a function that takes a string '(1,2,3,4,5),(5,4,3,2,1)' and returns a list of tuples of the 1st and last element of each tuple, [(1,5),(5,1)]. I was thinking: def f(givenstring): ...
Linus Svendsson's user avatar
41 votes
7 answers
26k views

C2977: 'std::tuple' : too many template arguments (MSVC11)

I'm trying to build googletest with Visual C++ 11, but following code causes an error template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, ...
Loom's user avatar
  • 9,918
41 votes
6 answers
17k views

How do I create dictionary from array of tuples?

Let’s say I have array of objects that can be identified and I want to create dictionary from it. I can easily get tuples from my array like so: let tuples = myArray.map { return ($0.id, $0) } But I ...
Adamsor's user avatar
  • 760
41 votes
5 answers
83k views

What is the best way to check if a tuple has any empty/None values in Python?

What is the best/most efficient way to check if all tuple values? Do I need to iterate over all tuple items and check or is there some even better way? For example: t1 = (1, 2, 'abc') t2 = ('', 2, 3)...
Andrius's user avatar
  • 20.5k
41 votes
1 answer
23k views

How does swapping of members in tuples (a,b)=(b,a) work internally?

In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of values of a and b work internally? Its definitely not using a temp ...
praveen's user avatar
  • 3,233
41 votes
4 answers
35k views

How to convert nested list of lists into a list of tuples in python 3.3?

I am trying to convert a nested list of lists into a list of tuples in Python 3.3. However, it seems that I don't have the logic to do that. The input looks as below: >>> nested_lst = [['...
Mohammed's user avatar
  • 1,364
41 votes
7 answers
41k views

In Haskell, how can I use the built in sortBy function to sort a list of pairs(tuple)?

I am a beginner in Haskell so please bear with me. (Just started learning yesterday!) How can I sort a list of tuples primarily by their first components (highest to smallest) and secondarily by their ...
eclipseNoob's user avatar
40 votes
3 answers
43k views

How does Python's comma operator work during assignment?

I was reading the assignment statements in the Python docs ( http://docs.python.org/reference/simple_stmts.html#assignment-statements). In that it is quoted that: If the target is a target list ...
thiruvenkadam's user avatar
40 votes
3 answers
245k views

django: TypeError: 'tuple' object is not callable

Getting a type error, 'tuple' object is not callable. Any idea what it could be? (dont worry about the indentation. It copies in weird.) I'm trying to create choices based on PackSize of storeliquor. ...
RuSs's user avatar
  • 789
40 votes
4 answers
15k views

Convert an array of tuples into a hash-map in Clojure

I have an array of tuples, where each tuple is a 2 tuple with a key and a value. What would be the cleanest way to convert this array of tuples into a hash-map?
Mantas Vidutis's user avatar