![]() |
VOOZH | about |
Given list of tuples, extract Kth column element of every Nth tuple.
Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 2, N = 3
Output : [3, 8, 10]
Explanation : From 0th, 3rd, and 6th tuple, 2nd elements are 3, 8, 10.Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 0, N = 3
Output : [4, 4, 1]
Explanation : From 0th, 3rd, and 6th tuple, 0th elements are 4, 4, 1.
Method #1 : Using loop
In this, we iterate by skipping N values using 3rd parameter of range(), and append every Kth value in list using loop.
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)] The extracted elements : [5, 7, 9]
Time Complexity: O(n), where n is the elements of tuple
Auxiliary Space: O(n), where n is the size of tuple
Method #2 : Using list comprehension
In this, we perform task of extracted elements using one-liner using list comprehension, using same method as above.
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)] The extracted elements : [5, 7, 9]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #3: Using enumerate
This code extracts the Kth element from every Nth tuple in the list test_list. Here's the step-by-step algorithm for this approach:
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)] The extracted elements : [5, 7, 9]
Time complexity: O(n), where n is the number of tuples in the list test_list.
Auxiliary space: O(n), since we are creating a new list to store the extracted elements.
Method 4: Using the map() function with a lambda expression.
Step-by-step approach:
Below is the implementation of the above approach:
The original list is: [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)] The extracted elements: [5, 7, 9]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n) as we are creating a new list to store the extracted elements.
Method 5: Using numpy array indexing
Step-by-step approach:
OUTPUT:
The original list is: [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)] The extracted elements: [5, 7, 9]
The time complexity of this method is O(N), where N is the length of the original list.
The auxiliary space is O(N) as well, because we create a new numpy array to store the data.
Method 6: Using a generator expression with the yield keyword.
Step-by-step approach:
Below is the implementation of the above approach:
The original list is: [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)] The extracted elements: [5, 7, 9]
Time complexity: O(N), where N is the length of the list divided by the step size N.
Auxiliary space: O(1), as only constant amount of extra space is used.