How to extract the nth element from a list of tuples?

I am trying to get the nth elements from a list of tuples.

I have something like:

elements = [(1,1,1),(2,3,7),(3,5,10)] 

I want to extract only two elements of each tuple in the list:

 seconds = [1, 3, 5] 

I know this can be done with a for loop, but I wanted to know if there is another way, since I have thousands of tuples.

+78
python list tuples
Jul 22 2018-10-22T00:
source share
7 answers
 [x[1] for x in elements] 
+147
Jul 22 2018-10-22T00:
source share
โ€” -

I know this can be done with FOR, but I wanted to know if there is another way

There is another way. You can also do this with map and itemgetter :

 >>> from operator import itemgetter >>> map(itemgetter(1), elements) 

This still runs the loop inside, although a bit slower than comprehending the list:

 setup = 'elements = [(1,1,1) for _ in range(100000)];from operator import itemgetter' method1 = '[x[1] for x in elements]' method2 = 'map(itemgetter(1), elements)' import timeit t = timeit.Timer(method1, setup) print('Method 1: ' + str(t.timeit(100))) t = timeit.Timer(method2, setup) print('Method 2: ' + str(t.timeit(100))) 

Results:

 Method 1: 1.25699996948
 Method 2: 1.46600008011

If you need to iterate over the list, then using for is fine.

+26
Jul 22 2018-10-22T00:
source share

This also works:

 zip(*elements)[1] 

(I mainly post this to prove to myself that I stabbed a zip ...)

See it in action:

 >>> help(zip) 

Help for the built-in zip function in the built-in module:

zipper (...)

zip (seq1 [, seq2 [...]]) โ†’ [(seq1 [0], seq2 [0] ...), (...)]

Return a list of tuples, where each tuple contains the ith element from each sequence of arguments. The returned list is truncated to the length of the shortest sequence of arguments.

 >>> elements = [(1,1,1),(2,3,7),(3,5,10)] >>> zip(*elements) [(1, 2, 3), (1, 3, 5), (1, 7, 10)] >>> zip(*elements)[1] (1, 3, 5) >>> 

The nifty thing I found out today: use *list in the arguments to create a list of parameters for the function ...

Note. In Python3, zip returns an iterator, so use list(zip(*elements)) instead to return a list of tuples.

+24
Jul 22 '10 at 12:29
source share

Found this when I was looking for which way to quickly get the second item from a list of 2 tuples. Not what I wanted, but performed the same tests as the third, plus a zip method test

 setup = 'elements = [(1,1) for _ in range(100000)];from operator import itemgetter' method1 = '[x[1] for x in elements]' method2 = 'map(itemgetter(1), elements)' method3 = 'dict(elements).values()' method4 = 'zip(*elements)[1]' import timeit t = timeit.Timer(method1, setup) print('Method 1: ' + str(t.timeit(100))) t = timeit.Timer(method2, setup) print('Method 2: ' + str(t.timeit(100))) t = timeit.Timer(method3, setup) print('Method 3: ' + str(t.timeit(100))) t = timeit.Timer(method4, setup) print('Method 4: ' + str(t.timeit(100))) Method 1: 0.618785858154 Method 2: 0.711684942245 Method 3: 0.298138141632 Method 4: 1.32586884499 

So it is twice as fast if you have a pair of 2 tuples to just convert to dict and take values.

+11
Nov 20 '12 at 9:50
source share
 map (lambda x:(x[1]),elements) 
+3
Oct 08 '14 at
source share

Times for Python 3.6 to extract the second element from a list of two tuples.

In addition, a numpy array method has been added, which is easier to read (but perhaps simpler than list comprehension).

 from operator import itemgetter elements = [(1,1) for _ in range(100000)] %timeit second = [x[1] for x in elements] %timeit second = list(map(itemgetter(1), elements)) %timeit second = dict(elements).values() %timeit second = list(zip(*elements))[1] %timeit second = np.array(elements)[:,1] 

and terms:

 list comprehension: 4.73 ms ยฑ 206 ยตs per loop list(map): 5.3 ms ยฑ 167 ยตs per loop dict: 2.25 ms ยฑ 103 ยตs per loop list(zip) 5.2 ms ยฑ 252 ยตs per loop numpy array: 28.7 ms ยฑ 1.88 ms per loop 

Note that map() and zip() no longer return a list, hence an explicit conversion.

+2
Jan 05 '18 at 12:42
source share

Using islice and chain.from_iterable :

 >>> from itertools import chain, islice >>> elements = [(1,1,1),(2,3,7),(3,5,10)] >>> list(chain.from_iterable(islice(item, 1, 2) for item in elements)) [1, 3, 5] 

This can be useful when you need more than one element:

 >>> elements = [(0, 1, 2, 3, 4, 5), (10, 11, 12, 13, 14, 15), (20, 21, 22, 23, 24, 25)] >>> list(chain.from_iterable(islice(tuple_, 2, 5) for tuple_ in elements)) [2, 3, 4, 12, 13, 14, 22, 23, 24] 
0
Jan 27 '19 at 15:27
source share



All Articles