Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Find Kth Even Element

  • Last Updated : 02 Sep, 2020

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!