When it is required to find the ‘K’th even element in a list, a list comprehension and the ‘%’ operator is used.
Example
Below is a demonstration of the same −
my_list = [14, 63, 28, 32, 18, 99, 13, 61]
print("The list is :")
print(my_list)
K = 3
print("The value of K is :")
print(K)
my_result = [element for element in my_list if element % 2 == 0][K]
print("The result is :")
print(my_result)Output
The list is : [14, 63, 28, 32, 18, 99, 13, 61] The value of K is : 3 The result is : 18
Explanation
A list of integers is defined and is displayed on the console.
A value for K is defined and is displayed on the console.
A list comprehension is used to iterate over the list, and every element is divided by 2 and its remainder is compared with 0.
If it is 0, it is added to a list, and is assigned to a variable.
This is the output that is displayed on the console.