Get first element from a List of tuples - Python
Last Updated :
22 Apr, 2025
The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of efficiency and readability. Let's explore different approaches to accomplish this task.
Using list comprehension
List comprehension is considered one of the most efficient way to iterate over a collection and transform elements, all in a single line.
Python
a = [(1, 'sravan'), (2, 'ojaswi'),(3, 'bobby'), (4, 'rohith'),(5, 'gnanesh')]
res = [x[0] for x in a]
print(res)
Explanation: [x[0] for x in a] iterates over each tuple in the list "a", extracts the first element x[0] from each tuple and creates a new list res containing these first elements.
Using for loop
For loop is efficient and easy to understand but may have slightly more overhead due to the explicit iteration and function calls.
Python
a = [(1, 'sravan'), (2, 'ojaswi'),(3, 'bobby'), (4, 'rohith'),(5, 'gnanesh')]
for x in a:
print(x[0])
Explanation: For loop iterates through each tuple in the list a, accesses the first element (x[0]) and prints it.
Using map()
map() applies a function to all elements in the list. This is a bit more functional, but less intuitive than a loop or comprehension.
Python
a = [(1, 'sravan'), (2, 'ojaswi'),(3, 'bobby'), (4, 'rohith'),(5, 'gnanesh')]
res = list(map(lambda x: x[0], a))
print(res)
Explanation: map() function applies lambda x: x[0] to each tuple in a, extracting the first element. The iterator returned by map() is then converted to a list using list() and stored in res.
Using generator expression
A generator expression is like a list comprehension but without creating an entire list in memory, which can be useful for large datasets.
Python
a = [(1, 'sravan'), (2, 'ojaswi'),(3, 'bobby'), (4, 'rohith'),(5, 'gnanesh')]
res = (x[0] for x in a)
for i in res:
print(i)
Explanation: (x[0] for x in a) creates an iterator that yields the first element of each tuple in a, generating values one at a time without storing the entire list in memory.
Similar Reads:
Similar Reads
Python - Filter Tuples by Kth element from List Given a list of tuples, filter by Kth element presence in List. Input : test_list = [("GFg", 5, 9), ("is", 4, 3), ("best", 10, 29)], check_list = [4, 2, 3, 10], K = 2 Output : [('is', 4, 3)] Explanation : 3 is 2nd element and present in list, hence filtered tuple. Input : test_list = [("GFg", 5, 9),
5 min read
Python | Get first element of each sublist Given a list of lists, write a Python program to extract first element of each sublist in the given list of lists. Examples: Input : [[1, 2], [3, 4, 5], [6, 7, 8, 9]] Output : [1, 3, 6] Input : [['x', 'y', 'z'], ['m'], ['a', 'b'], ['u', 'v']] Output : ['x', 'm', 'a', 'u'] Â Approach #1 : List compre
3 min read
Removing Tuples from a List by First Element Value - Python In this problem we need to delete tuples based on a specific condition related to their first element. For example: We are given the list data = [("GeeksforGeeks", "Python", 1000), ("CodingForAll", "Java", 1200)] and we need to remove all tuples where the first element is "GeeksforGeeks", the desire
3 min read
Extract Elements from a Python List When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print(
2 min read
Python - Rear element extraction from list of tuples records While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read