Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Get First Element from Each Tuple in a List of Tuples in Python

  • 22 Mar, 2025
  • Com 0
Getting First Elements from Each Tuple in a List of Tuples in Python

The fastest, most reliable, and concise method to get the first element from each tuple in the list of tuples is to use list comprehension. It will return a list containing the first element of each tuple.

For example, if I have a list [(1,2), (3,4), (5, 6)], then list comprehension will return [1, 3, 5].

Efficient way: List comprehension

We have to create a new list of the first elements of each tuple out of an existing input list, and that’s why list comprehension is the efficient way.

Using list comprehension to get the first elements of each tuple from a list of tuples

list_of_tuples = [
    (1, "Krunal", 25.5), 
    (2, "Ankit", 30.0), 
    (3, "Vidisha", 22.3)
]

first_elements = [t[0] for t in list_of_tuples]

print(first_elements)

# Output: [1, 2, 3]

What if a list is empty?

One of the main advantages of using list comprehension is that if the list is empty, it will return an empty list instead of throwing an error because it never enters the iteration. This makes it safer than direct access using indexing.

list_of_tuples = []

first_elements = [t[0] for t in list_of_tuples]

print(first_elements)

# Output: []

What if some tuples are empty in a list?

If the whole list is empty, then list comprehension will handle it, but if some tuples are empty and others are not, that becomes tricky, and that’s where it throws an error: IndexError: tuple index out of range

list_of_tuples = [(1, 2), (), (3, 4), ()]

first_elements = [t[0] for t in list_of_tuples]

print(first_elements)

# IndexError: tuple index out of range

You can handle this scenario using the if condition in list comprehension like this:

list_of_tuples = [(1, 2), (), (3, 4), ()]

first_elements = [t[0] for t in list_of_tuples if t]

print(first_elements)

# Output: [1, 3]

A feasible solution is ensuring all the tuples have one element guaranteed.

Using operator.itemgetter() with map()

The itemgetter() function from the operator module gets the element from the specified index in an iterable.

With the help of the map() function, we apply the itemgetter(0) function to each tuple, creating an iterator of the first elements. To convert that iterator to a list, we will use the list() constructor.

Using itemgetter() and map() functions

from operator import itemgetter

list_of_tuples = [
    (1, "Krunal", 25.5), 
    (2, "Ankit", 30.0), 
    (3, "Vidisha", 22.3)
]

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

print(first_elements)

# Output: [1, 2, 3]

If you pass an empty list, it will throw an IndexError exception.

Using lambda with map()

Instead of using the itemgetter() function, we use lambda t: t[0] to extract the first element from each tuple.

The map() function applies this lambda function to each tuple in the list, returning an iterator of the first elements. We use the list() constructor to convert this iterator into a list.

Using lambda and map() functions

list_of_tuples = [
    (1, "Krunal", 25.5), 
    (2, "Ankit", 30.0), 
    (3, "Vidisha", 22.3)
]

first_elements = list(map(lambda t: t[0], list_of_tuples))

print(first_elements)

# Output: [1, 2, 3]

It has the same error-handling issues as other methods.

Using zip()

The zip() function returns a zip object, an iterator of the tuple.

The list(zip(*list_of_tuples))[0] will return a tuple of the first elements of each tuple from a list.

Using zip()

list_of_tuples = [
    (1, "Krunal", 25.5), 
    (2, "Ankit", 30.0), 
    (3, "Vidisha", 22.3)
]

first_elements = list(zip(*list_of_tuples))[0]

print(first_elements)

# Output: (1, 2, 3) # Output is in tuple instead of list

The zip() approach is highly concise and efficient for small datasets if all tuples are of equal length.

You can use the manual for loop approach here, but it is unnecessary when you have more concise list comprehension.

Post Views: 27
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Count the Number of None and NaN Values in Python List
How to Find the Length of a List in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend