Python – Find Kth Even Element

Finding the Kth even element in a list is a common programming task. Python provides several approaches using list comprehension, filtering, and indexing techniques.

Method 1: Using List Comprehension

Create a list of even elements and access the Kth element directly ?

numbers = [14, 63, 28, 32, 18, 99, 13, 61]

print("The list is:")
print(numbers)

K = 3
print("The value of K is:")
print(K)

# Find Kth even element (1-indexed)
result = [element for element in numbers if element % 2 == 0][K-1]

print("The Kth even element is:")
print(result)
The list is:
[14, 63, 28, 32, 18, 99, 13, 61]
The value of K is:
3
The Kth even element is:
28

Method 2: Using Filter and List Conversion

Use the built-in filter() function for cleaner code ?

numbers = [14, 63, 28, 32, 18, 99, 13, 61]
K = 2

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", even_numbers)

kth_even = even_numbers[K-1]
print(f"The {K}th even element is: {kth_even}")
Even numbers: [14, 28, 32, 18]
The 2nd even element is: 28

Method 3: Iterative Approach with Counter

Use a counter to find the Kth even element without creating intermediate lists ?

def find_kth_even(numbers, k):
    count = 0
    for num in numbers:
        if num % 2 == 0:
            count += 1
            if count == k:
                return num
    return None

numbers = [14, 63, 28, 32, 18, 99, 13, 61]
K = 4

result = find_kth_even(numbers, K)
if result is not None:
    print(f"The {K}th even element is: {result}")
else:
    print(f"There are fewer than {K} even elements in the list")
The 4th even element is: 18

Comparison

Method Memory Usage Best For
List Comprehension High (creates full list) Small lists, readable code
Filter Function High (creates full list) Functional programming style
Iterative Approach Low (constant space) Large lists, memory efficiency

Conclusion

Use list comprehension for simple cases with small datasets. For memory efficiency with large lists, prefer the iterative approach that stops once the Kth even element is found.

Updated on: 2026-03-26T01:17:13+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements