Open In App

Python – Find Kth Even Element

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, extract Kth occurrence of Even Element.

Input : test_list = [4, 6, 2, 3, 8, 9, 10, 11], K = 3 
Output : 8 
Explanation : K = 3, i.e 0 based index, 4, 6, 2 and 4th is 8. 

Input : test_list = [4, 6, 2, 3, 8, 9, 10, 11], K = 2 
Output : 2 
Explanation : K = 2, i.e 0 based index, 4, 6, and 3rd is 2.

Method #1 : Using list comprehension

In this, we extract list of even elements using % operator and use list index access to get Kth even element.

Python3




# Python3 code to demonstrate working of
# Kth Even Element
# Using list comprehension
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# list comprehension to perform iteration and % 2 check
res = [ele for ele in test_list if ele % 2 == 0][K]
 
# printing result
print("The Kth Even Number : " + str(res))


Output

The original list is : [4, 6, 2, 3, 8, 9, 10, 11]
The Kth Even Number : 10

Time Complexity: O(n), where n is the elements of list
Auxiliary Space: O(n), where n is the size of list

Method #2 : Using filter() + lambda

In this, task of finding even elements is done using filter() + lambda function. 

Python3




# Python3 code to demonstrate working of
# Kth Even Element
# Using filter() + lambda
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# list comprehension to perform iteration and % 2 check
res = list(filter(lambda ele : ele % 2 == 0, test_list))[K]
 
# printing result
print("The Kth Even Number : " + str(res))


Output

The original list is : [4, 6, 2, 3, 8, 9, 10, 11]
The Kth Even Number : 10

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 for loop +copy()+remove() methods

Python3




# Python3 code to demonstrate working of
# Kth Even Element
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
for i in test_list.copy():
    if i % 2 != 0:
        test_list.remove(i)
 
 
res = test_list[K]
# printing result
print("The Kth Even Number : " + str(res))


Output

The original list is : [4, 6, 2, 3, 8, 9, 10, 11]
The Kth Even Number : 10

Time Complexity:O(n)
Auxiliary Space: O(n)

Method #4:Using itertools.filterfalse() method

Python3




# Python3 code to demonstrate working of
# Kth Even Element
import itertools
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
res = list(itertools.filterfalse(lambda ele : ele % 2 != 0, test_list))[K]
 
# printing result
print("The Kth Even Number : " + str(res))


Output

The original list is : [4, 6, 2, 3, 8, 9, 10, 11]
The Kth Even Number : 10

Time Complexity:O(N)
Auxiliary Space: O(N)

Method #5: Using numpy:

Algorithm:

  1. Filter out the even numbers from the input list.
  2. Create a numpy array from the even numbers list.
  3. Use the numpy partition function to partition the array such that the first K elements are the K smallest elements.
  4. Return the Kth smallest element from the partitioned array.

Python3




import numpy as np
 
# initializing list
test_list = [4, 6, 2, 3, 8, 9, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# filtering even numbers and finding Kth smallest
even_list = np.array([i for i in test_list if i % 2 == 0])
res = np.partition(even_list, K-1)[K]
 
# printing result
print("The Kth Even Number : " + str(res))
 
#This code is contributed by Jyothi pinjala


Output:
The original list is : [4, 6, 2, 3, 8, 9, 10, 11]
The Kth Even Number : 10

Time Complexity:
The time complexity of this code is O(n log n), where n is the length of the input list. This is because numpy’s partition function uses the quickselect algorithm to select the Kth smallest element, which has a time complexity of O(n log n) in the worst case.

Space Complexity:
The space complexity of this code is O(n), where n is the length of the input list. This is because we are creating a new numpy array to store the even numbers from the input list.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads