Open In App

Python – Extract Kth element of every Nth tuple in List

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Python3




# Python3 code to demonstrate working of
# Extract Kth element of every Nth tuple in List
# Using loop
 
# initializing list
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)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# initializing N
N = 3
 
res = []
for idx in range(0, len(test_list), N):
     
    # extract Kth element
    res.append(test_list[idx][K])
 
# printing result
print("The extracted elements : " + str(res))


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.

Python3




# Python3 code to demonstrate working of
# Extract Kth element of every Nth tuple in List
# Using list comprehension
 
# initializing list
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)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# initializing N
N = 3
 
# Skipping N using 3rd param of range()
res = [test_list[idx][K] for idx in range(0, len(test_list), N)]
 
# printing result
print("The extracted elements : " + str(res))


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.

Python3




# initializing list
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)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# initializing N
N = 3
 
# Extract Kth element of every Nth tuple in List
res = [t[K] for i, t in enumerate(test_list) if i % N == 0]
 
# printing result
print("The extracted elements : " + str(res))
#This code is contributed by Vinay Pinjala.


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:

Python3




# initializing list
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)]
 
# printing original list
print("The original list is: " + str(test_list))
 
# initializing K
K = 1
 
# initializing N
N = 3
 
# Extract Kth element of every Nth tuple in List using map() and lambda expression
res = list(map(lambda t: t[K], test_list[::N]))
 
# printing result
print("The extracted elements: " + str(res))


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.

Python3




import numpy as np
 
# initializing list
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)]
 
# printing original list
print("The original list is: " + str(test_list))
 
# initializing K
K = 1
 
# initializing N
N = 3
 
# Convert the list to a numpy array
arr = np.array(test_list)
 
# Use array indexing to extract the Kth element of every Nth row
res = arr[::N, K].tolist()
 
# printing result
print("The extracted elements: " + str(res))


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:

Python3




# initializing list
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)]
 
# printing original list
print("The original list is: " + str(test_list))
 
# initializing K
K = 1
 
# initializing N
N = 3
 
# Define generator function to extract Kth element of every Nth tuple in list
def extract_tuple_element(lst, K, N):
    for i in range(0, len(lst), N):
        yield lst[i][K]
 
# Extract Kth element of every Nth tuple in List using generator expression
res = list(extract_tuple_element(test_list, K, N))
 
# printing result
print("The extracted elements: " + str(res))


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.



Last Updated : 10 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads