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
.
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.