Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Accessing nth element from tuples in list

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 21 Feb, 2023
Improve Article
Save Article

While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print a specific information from the tuple. For instance, a piece of code would want just names to be printed of all the student data. Lets discuss certain ways how one can achieve solutions to this problem. 

Method #1 : Using list comprehension List comprehension is the simplest way in which this problem can be solved. We can just iterate over only the specific index value in all the index and store it in a list and print it after that. 

Python3




# Python3 code to demonstrate
# get nth tuple element from list
# using list comprehension
 
# initializing list of tuples
test_list = [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list comprehension to get names
res = [lis[1] for lis in test_list]
     
# printing result
print ("List with only nth tuple element (i.e names) : " + str(res))

Output :

The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
List with only nth tuple element (i.e names) : ['Rash', 'Varsha', 'Kil']

Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(n), where n is the number of tuples in the input list. 

Method #2 : Using map() + itemgetter() map() coupled with itemgetter() can perform this task in more simpler way. map() maps all the element we access using itemgetter() and returns the result. 

Python3




# Python3 code to demonstrate
# get nth tuple element from list
# using map() + itemgetter()
from operator import itemgetter
 
# initializing list of tuples
test_list = [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using map() + itemgetter() to get names
res = list(map(itemgetter(1), test_list))
     
# printing result
print ("List with only nth tuple element (i.e names) : " + str(res))

Output :

The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
List with only nth tuple element (i.e names) : ['Rash', 'Varsha', 'Kil']

Method #3 : Using zip() The most conventional and pythonic way to perform this particular task is using this function. This collects all the nth tuple elements into a single container and returns the result. This only works for Python 2. 

Python




# Python code to demonstrate
# get nth tuple element from list
# using zip()
 
# initializing list of tuples
test_list = [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using zip() to get names
res = list(zip(*test_list)[1])
     
# printing result
print ("List with only nth tuple element (i.e names) : " + str(res))

Output :

The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
List with only nth tuple element (i.e names) : ['Rash', 'Varsha', 'Kil']

Method #4 : Using map() + lambda

To access the nth element of each tuple in a list using a lambda function, you can define a lambda function that returns the nth element of each tuple, and then use the map function to apply this lambda function to each tuple in the list.

Here is an example of how you can do this:

Python3




# initializing list of tuples
test_list = [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
 
# printing original list
print("The original list is:", test_list)
 
# using the map function and a lambda function to access the nth element of each tuple
n = 1  # specify the index of the element to access
res = list(map(lambda x: x[n], test_list))
 
# printing result
print("List with only nth tuple element:", res)
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original list is: [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)]
List with only nth tuple element: ['Rash', 'Varsha', 'Kil']

Time complexity: O(n), since it involves a single pass through the list. It uses the map function and a lambda function to access the nth element of each tuple, but does not require any additional modules.
Auxiliary space: O(n), since it requires the creation of a new list to store the result.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!