# Get the first element of each tuple in a list in Python

Last updated: Jun 30, 2022

Check out my new book

## Get the first element of each tuple in a list in Python#

Use a list comprehension to get the first element of each tuple in a list, e.g. `result = [tup[0] for tup in list_of_tuples]`. The list comprehension iterates over the list and returns the first element from each tuple.

main.py
```Copied!```list_of_tuples = [(1, 'a'), (2, 'b'), (3, 'c')]

result = [tup[0] for tup in list_of_tuples]

print(result)  # 👉️ [1, 2, 3]
``````

We used a list comprehension to get a new list that contains the first element of each tuple.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we access the tuple element at index `0` and return the result.

Python indexes are zero-based, so the first element in a tuple has an index of `0`, the second an index of `1`, etc.

Alternatively, you can use a `for` loop.

To get the first element of each tuple in a list:

1. Declare a new variable and set it to an empty list.
2. Use a `for` loop to iterate over the list of tuples.
3. On each iteration, append the first tuple element to the new list.
main.py
```Copied!```list_of_tuples = [(1, 'a'), (2, 'b'), (3, 'c')]

result = []

for tup in list_of_tuples:
result.append(tup[0])

print(result)  # 👉️ [1, 2, 3]
``````

We simply iterate over the list of tuples and append the first item of each tuple to a new list.

The list.append() method adds an item to the end of the list.

Alternatively, you can use the `map()` function.

main.py
```Copied!```from operator import itemgetter

list_of_tuples = [(1, 'a'), (2, 'b'), (3, 'c')]

result = list(map(itemgetter(0), list_of_tuples))

print(result)  # 👉️ [1, 2, 3]
``````

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The operator.itemgetter method returns a callable object that fetches the item at the specified index.

For example, `x = itemgetter(0)` and then calling `x(my_tuple)`, returns `my_tuple[0]`.

On each iteration, we get the first element of the tuple and return it.

The `map()` function returns a `map` object, so we had to use the `list()` class to convert the result to a list.

Which approach you pick is a matter of personal preference. I'd go with the list comprehension as I find it more direct and easier to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
You can use the search field on my Home Page to filter through all of my articles.