Home »
Python »
Python programs
Python program to extract rear element from list of tuples record
In this program, we are given a list of tuples where each tuple is a record consisting of some elements. We need to create a Python program to extract the last element of each tuple of the list.
Submitted by Shivang Yadav, on December 04, 2021
All the data that are collected every day are stored in complex data structures. And while processing these data records, we might need to extract some elements from our data extract information. In this program, we will be extracting rear elements from the given list of tuple records.
Before going further with the problem, let's recap some basic topics that will help in understanding the solution.
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
Extracting rear elements from list of tuples record
We have a list of tuples where each tuple contains some elements. We need to create a Python program to extract the last element of each tuple of the list.
Input:
[(1, 'python', 1991), (2, 'java', 1995), (3, 'C', 1972)]
Output:
1991, 1995, 1972
To find the solution to the problem, we will iterate over the array and then extract the last element of each tuple.
In Python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.
Method 1:
One method to solve the problem in Python is by using list comprehension using which we will iterate over the list and extract the value at index -1 for each tuple of the list and then print it.
# Python program to extract rear elements
# from list of tuples record
# initializing and printing list of Tuples
tupList = [(1, 'python', 1991), (2, 'java', 1995), (3, 'C', 1972)]
print ("The elements of list of tuples are " + str(tupList))
# extracting rear elements from the list of tuples
rearElementList = [tup[-1] for tup in tupList]
# printing result
print ("All rear elements of list of tuples are " + str(rearElementList))
Output:
The elements of list of tuples are [(1, 'python', 1991), (2, 'java', 1995), (3, 'C', 1972)]
All rear elements of list of tuples are [1991, 1995, 1972]
Method 2:
Another method to solve the problem is by using the map() method. And map all the last values of each tuple list which are extracted using the itemgetter() method.
# Python program to extract rear elements
# from list of tuples record
# initializing and printing list of Tuples
tupList = [(1, 'python', 1991), (2, 'java', 1995), (3, 'C', 1972)]
print ("The elements of list of tuples are " + str(tupList))
# extracting rear elements from the list of tuples
rearElementList = [tup[-1] for tup in tupList]
# printing result
print ("All rear elements of list of tuples are " + str(rearElementList))
Output:
The elements of list of tuples are [(1, 'python', 1991), (2, 'java', 1995), (3, 'C', 1972)]
All rear elements of list of tuples are [1991, 1995, 1972]
Python Tuple Programs »