![]() |
VOOZH | about |
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 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 rear index value in all the index and store it in a list and print it after that.
The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element : [21, 20, 19]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), as we are creating a new list to store the extracted elements.
Method #2: Using map() + itemgetter()
map() coupled with itemgetter() can perform this task in simpler way. map() maps all the element we access using itemgetter() and returns the result.
Follow the below steps to implement the above idea:
Below is the implementation of the above approach:
The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element : [21, 20, 19]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.
Method#3: Using the Recursive method.
The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element : [21, 20, 19]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4:Using enumerate
Approach:
Below is the implementation of the above approach:
The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element : [21, 20, 19]
Time complexity: O(nm), where n is the number of tuples in test_list and m is the length of the longest tuple. The enumerate() function and two nested loops iterate over all the elements in each tuple, so their time complexity is proportional to m. Therefore, the overall time complexity is O(nm).
Auxiliary space: O(n), where n is the number of tuples in test_list. The res list stores only the last element of each tuple, so its size is proportional to n. Therefore, the space complexity is O(n).
Method#5:Using numpy():
Algorithm:
Output:
The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element : ['21' '20' '19']
Time complexity: O(n), where n is the number of tuples in the input list. The numpy array conversion takes O(n) time, and slicing takes constant time.
Space complexity: O(n), where n is the number of tuples in the input list. This is because the input list is converted to a numpy array which takes O(n) space, and the resulting array containing only the last elements of each tuple also takes O(n) space.
Method #6: Using a for loop
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element : [21, 20, 19]
Time complexity: O(n), where n is the length of test_list because we iterate over each element in the list once.
Auxiliary space: O(n), where n is the length of test_list because we create a new list called res with n elements.
Method 6: Using Loops
The original list is: [(1, 'Rash', 21), (2, 'Varsha', 20), (3, 'Kil', 19)] List with only rear tuple element: [21, 20, 19]
Time complexity: O(n) where n is the length of the input list
Auxiliary space: O(n) where n is the length of the input list, for the output list.