VOOZH about

URL: https://www.geeksforgeeks.org/python/python-extract-kth-element-of-every-nth-tuple-in-list/

⇱ Python - Extract Kth element of every Nth tuple in List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Extract Kth element of every Nth tuple in List

Last Updated : 10 Apr, 2023

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:

  • Create an empty list named res.
  • Start a loop using the range() function to iterate over the indices of test_list, with a step of N.
    • Extract the Kth element from the tuple at the current index, and append it to res.
  • Print the extracted elements.

Below is the implementation of the above 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]

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.


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]

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:

  1. Initialize the list test_list of tuples
  2. Initialize the values of K and N
  3. Use a list comprehension to iterate through the tuples in test_list.
  4. Check if the index of the current tuple is divisible by N using the modulo operator %. If the index is not divisible by N, skip this iteration.
  5. If the index is divisible by N, extract the Kth element of the tuple and add it to the result list.
  6. Return the result list.

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]

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:

  1. Initialize the list of tuples:
  2. Initialize the values of K and N
  3. Use the map() function with a lambda expression to extract the Kth element of every Nth tuple in the 
  4. Print the extracted elements

Below is the implementation of the above 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]

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:

  1. Convert the list of tuples to a numpy array using the numpy.array() function.
  2. Use array indexing to extract the Kth element of every Nth row in the array.
  3. Convert the resulting numpy array to a Python list using the .tolist() method.

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:

  • Define a generator function that takes in the list, K and N as parameters.
  • Use a loop to iterate through the indices of the list from 0 to the length of the list, incrementing by N.
  • Use the yield keyword to return the Kth element of the tuple at the current index.
  • Call the generator function with the test list, K, and N.
  • Convert the generator object to a list using the list() function and assign it to the variable res.
  • Print the extracted elements using the print() function and string concatenation.

Below is the implementation of the above 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]

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.

Comment