In this tutorial, we will look at how to get the first element of each tuple from a list of tuples in Python with the help of some examples.

Let’s look at some methods with the help of examples.
Using a Loop to get first element of each tuple
The straightforward way is to loop over each tuple in the list and append its first element to our result list or tuple.
# list of tuples employees = [("Jim", "Scranton"), ("Ryan", "New York"), ("Dwight", "Scranton")] # get first element of each tuple names = [] for e in employees: names.append(e[0]) # display the result print(names)
Output:
['Jim', 'Ryan', 'Dwight']
We get a list of the first items in each tuple. The resulting list names
contains the names from each tuple in the list employees
.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Using List Comprehension
You can also use list comprehension to reduce the above code (to get the first element of each tuple in Python) to a single line.
# list of tuples employees = [("Jim", "Scranton"), ("Ryan", "New York"), ("Dwight", "Scranton")] # get first element of each tuple names = [e[0] for e in employees] # display the result print(names)
Output:
['Jim', 'Ryan', 'Dwight']
We get the same result as above.
You can similarly use the above methods to get the last element in each tuple. Use the -1
index to access the last element from a tuple. Let’s use the list comprehension method to get a list of all the last elements from each tuple in the original list.
# list of tuples employees = [("Jim", "Scranton"), ("Ryan", "New York"), ("Dwight", "Scranton")] # get last element of each tuple city = [e[-1] for e in employees] # display the result print(city)
Output:
['Scranton', 'New York', 'Scranton']
You can see that the new list has only the last elements from the tuples in the original list. The resulting list city
contains the name of the city for each employee in the list employee
.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.