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.
Output:
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.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer