0

If I have, for example, a list of tuples such as

a = [(1,2)] * 4

how would I create a list of the first element of each tuple? That is, [1, 1, 1, 1].

6 Answers 6

7

Use a list comprehension:

>>> a = [(1,2)] * 4
>>> [t[0] for t in a]
[1, 1, 1, 1]

You can also unpack the tuple:

>>> [first for first,second in a]
[1, 1, 1, 1]

If you want to get fancy, combine map and operator.itemgetter. In python 3, you'll have to wrap the construct in list to get a list instead of an iterable:

>>> import operator
>>> map(operator.itemgetter(0), a)
<map object at 0x7f3971029290>
>>> list(map(operator.itemgetter(0), a))
[1, 1, 1, 1]
Sign up to request clarification or add additional context in comments.

1 Comment

The tuple-unpacking option is fastest (on my Python 2.7, using a million-element list with no structure sharing in the tuples). The differences are quite small, though.
6

Two alternatives to phihag's list comprehension:

[x for x, y in a]

from operator import itemgetter
map(itemgetter(0), a)

Comments

4

There are several ways:

>>> a = [(1,2)] * 4

>>> # List comprehension
>>> [x for x, y in a]
[1, 1, 1, 1]

>>> # Map and lambda
>>> map(lambda t: t[0], a)
[1, 1, 1, 1]

>>> # Map and itemgetter
>>> import operator
>>> map(operator.itemgetter(0), a)
[1, 1, 1, 1]

The technique of using map fell out of favor when list comprehensions were introduced, but now it is making a comeback due to parallel map/reduce and multiprocessing techniques:

>>> # Multi-threading approach
>>> from multiprocessing.pool import ThreadPool as Pool
>>> Pool(2).map(operator.itemgetter(0), a)
[1, 1, 1, 1]

>>> # Multiple processes approach
>>> from multiprocessing import Pool
>>> def first(t):
        return t[0]
>>> Pool(2).map(first, a)
[1, 1, 1, 1]

6 Comments

+1 for the most over-engineered code to work on [(1,2)] * 4.
@phihag Never miss an opportunity to use simple examples as way to introduce a full range of Python capabilities :-) I presume the OP will have more interesting datasets to work with ;-)
@Raymond Hettinger you are correct. In practice I have a list of over 2 million such tuples.
@user1475412 If they're indeed tuples, you won't get any performance advantage by using multiple threads or multiple processes, because the cost of calling operator.itemgetter(0) is minimal. A multithreaded approach is only useful if the operation you're applying on each element is slow.
@RaymondHettinger The last example fails on my system with a rather curious error.
|
0
a = [(1,2)] * 4
first_els = [x[0] for x in a]

1 Comment

Don't use tuple as a variable name. In particular don't use it as the name of a list!
0

Assuming you have a list of tuples:

lta = [(1,2), (2,3), (44,45), (37,38)]

access the first element of each tuple would involve subscripting with [0], and visiting each tuple to extract each first element would involve a a list comprehension, which can be assigned to a variable as shown below:

resultant_list = [element[0] for element in lta]
>>> resultant_list
[1, 2, 44, 37]

Comments

0

I recently found out about Python's zip() function. Another way to do what I want to do here is:

list( zip( *a )[0] )

tup_list = zip( list1, list2 ) interleaves two lists into a list of 2-tuples, but zip( *tup_list ) does the opposite, resulting in a list of a tuple of list1 and a tuple of list2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.