Python – Get first element from a List of tuples
In this article, we will discuss how to get the first element in the list of tuples in Python.
Tuples are an immutable data structure in Python, meaning we can not update its state after creation. Lists are similar to tuples but they are mutable data structures.
Create a list of Tuples
Python3
# create tuples with college # id and name and store in a list data = [( 1 , 'sravan' ), ( 2 , 'ojaswi' ), ( 3 , 'bobby' ), ( 4 , 'rohith' ), ( 5 , 'gnanesh' )] # display data print (data) |
[(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby'), (4, 'rohith'), (5, 'gnanesh')]
How to Access First Element from a List of Tuples
There are different methods to access the first element from a list of Tuples in Python. We have discussed them below with examples for better understanding.
Method 1: Using iteration (for loop)
We can iterate through the entire list of tuples and get first by using the index, index-0 will give the first element in each tuple in a list.
Syntax:
for var in list_of_tuple:
print(var[0])
Example: Here we will get the first IE student id from the list of tuples.
Python3
# create tuples with college id # and name and store in a list data = [( 1 , 'sravan' ), ( 2 , 'ojaswi' ), ( 3 , 'bobby' ), ( 4 , 'rohith' ), ( 5 , 'gnanesh' )] # iterate using for loop # to access first elements for i in data: print (i[ 0 ]) |
1 2 3 4 5
Method 2: Using zip function
Zip is used to combine two or more data/data structures. Here we can use the zip() function by using the index as 0 to get the first element.
Syntax: list(zip(*data))[0]
Python3
# create tuples with college id # and name and store in a list data = [( 1 , 'sravan' ), ( 2 , 'ojaswi' ), ( 3 , 'bobby' ), ( 4 , 'rohith' ), ( 5 , 'gnanesh' )] # get first element using zip print ( list ( zip ( * data))[ 0 ]) |
(1, 2, 3, 4, 5)
Method 3: Using map() with itemgetter() method
Here we are using the map function along with the itemgetter() method to get the first elements, here also we are using index – 0 to get the first elements. itemgetter() method is available in the operator module, so we need to import the operator
Syntax: map(operator.itemgetter(0), data)
Python3
import operator # create tuples with college id # and name and store in a list data = [( 1 , 'sravan' ), ( 2 , 'ojaswi' ), ( 3 , 'bobby' ), ( 4 , 'rohith' ), ( 5 , 'gnanesh' )] # map the data using item # getter method with first elements first_data = map (operator.itemgetter( 0 ), data) # display first elements for i in first_data: print (i) |
1 2 3 4 5
Method 4: Using map() with Lambda Expression
Here we are using lambda expression along with map() function, here also we are using index 0 to get the first data.
Python3
# create tuples with college id # and name and store in a list data = [( 1 , 'sravan' ),( 2 , 'ojaswi' ), ( 3 , 'bobby' ),( 4 , 'rohith' ), ( 5 , 'gnanesh' )] # map with lambda expression to get first element first_data = map ( lambda x: x[ 0 ], data) # display data for i in first_data: print (i) |
1 2 3 4 5
Method 5: Using List Comprehension
First create a list of tuples called data. Now use a List Comprehension to extract the first element of each tuple. Store it in a new list called result. Finally, it prints the result list.
Python3
# create tuples with college id and name and store in a list data = [( 1 , 'sravan' ), ( 2 , 'ojaswi' ), ( 3 , 'bobby' ),( 4 , 'rohith' ),( 5 , 'gnanesh' )] # Accessing first elements of each tuple result = [t[ 0 ] for t in data] print (result) |
[1, 2, 3, 4, 5]
Time complexity: O(n) (where n is the number of tuples in the list)
Auxiliary Space: O(n) (where n is the number of tuples in the list) since we are storing the result in a list.
We have provided you with 5 easy methods to get the first element from a list of tuples in Python. Accessing data from a data structure is a very basic operation and should be known if you are learning Python.
Similar Reads:
- Create a List of Tuples
- Create list of tuples using for loop
- Accessing nth element from tuples in list