25

Sorry in advance, but I'm new to Python. I have a list of tuples, and I was wondering how I can reference, say, the first element of each tuple within the list. I would think it's something like

for i in number_of_tuples :
  first_element = myList[i[0]]

you know, [list_element[tuple_element]]? However, this doesn't appear to be the right approach. Any help would be greatly appreciated.

Thanks,

Turner

Sachin Chavan
  • 5,578
  • 5
  • 49
  • 75
Turner Hughes
  • 341
  • 2
  • 4
  • 5

8 Answers8

48

All of the other answers here are correct but do not explain why what you were trying was wrong. When you do myList[i[0]] you are telling Python that i is a tuple and you want the value or the first element of tuple i as the index for myList.

In the majority of programming languages when you need to access a nested data type (such as arrays, lists, or tuples), you append the brackets to get to the innermost item. The first bracket gives you the location of the tuple in your list. The second bracket gives you the location of the item in the tuple.

This is a quick rudimentary example that I came up with:

info = [ ( 1, 2), (3, 4), (5, 6) ]

info[0][0] == 1
info[0][1] == 2
info[1][0] == 3
info[1][1] == 4
info[2][0] == 5
info[2][1] == 6
simurg
  • 1,208
  • 7
  • 14
Prodigal Maestro
  • 613
  • 6
  • 11
  • I wonder whether this answer has a higher computational cost than the list comprehension answer? The latter certainly has a higher memory cost! – Serge Stroobandt May 23 '15 at 13:32
31

You can get a list of the first element in each tuple using a list comprehension:

>>> my_tuples = [(1, 2, 3), ('a', 'b', 'c', 'd', 'e'), (True, False), 'qwerty']
>>> first_elts = [x[0] for x in my_tuples]
>>> first_elts
[1, 'a', True, 'q']
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • what if I want to access all the elements of the tuple, like I have a list = [(1,2), (3,4) ,(5,6)]. I want to get 1, 2 both and then 3,4 and so on.. And not just the first or the last element. – Liza Jun 05 '17 at 19:36
  • @Liza `for x in my_tuples`...? – Matt Ball Jun 05 '17 at 19:50
  • https://stackoverflow.com/questions/27454390/how-to-flatten-a-list-of-tuples-into-a-pythonic-list – Francois Jun 19 '19 at 12:57
5

The code

my_list = [(1, 2), (3, 4), (5, 6)]
for t in my_list:
    print t

prints

(1, 2)
(3, 4)
(5, 6)

The loop iterates over my_list, and assigns the elements of my_list to t one after the other. The elements of my_list happen to be tuples, so t will always be a tuple. To access the first element of the tuple t, use t[0]:

for t in my_list:
    print t[0]

To access the first element of the tuple at the given index i in the list, you can use

print my_list[i][0]
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
4

You also can use itemgetter operator:

from operator import itemgetter
my_tuples = [('c','r'), (2, 3), ('e'), (True, False),('text','sample')]
map(itemgetter(0), my_tuples)
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
3

Rather than:

first_element = myList[i[0]]

You probably want:

first_element = myList[i][0]
Edward Loper
  • 15,374
  • 7
  • 43
  • 52
1

So you have "a list of tuples", let me assume that you are manipulating some 2-dimension matrix, and, in this case, one convenient interface to accomplish what you need is the one numpy provides.

Say you have an array arr = numpy.array([[1, 2], [3, 4], [5, 6]]), you can use arr[:, 0] to get a new array of all the first elements in each "tuple".

satoru
  • 31,822
  • 31
  • 91
  • 141
1

Here's a quick example:

termList = []
termList.append(('term1', [1,2,3,4]))
termList.append(('term2', [5,6,7,8]))
termList.append(('term3', [9,10,11,12]))

result = [x[1] for x in termList if x[0] == 'term3']

print(result)
MateusR
  • 101
  • 5
0

With my approach you do not need to import any module, it is more pythonic since it uses python´s native features:

list_of_tuples = [('a1', 'b1','c1'), ('a2', 'b2','c2'), ('a3', 'b3','c3')]

print(list(map(lambda x: x[0], list_of_tuples)))
>> ['a1', 'a2', 'a3']

print(list(map(lambda x: x[2], list_of_tuples)))
>> ['c1', 'c2', 'c3']

By following this logic you can carry out other operations such as getting a particular item from a list of dictionaries, etc.