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.
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.
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.
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.
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.