Python - Find Kth Even Element
Last Updated :
30 Mar, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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:
- Filter out the even numbers from the input list.
- Create a numpy array from the even numbers list.
- Use the numpy partition function to partition the array such that the first K elements are the K smallest elements.
- 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.
Similar Reads
Python - Get Indices of Even Elements from list Sometimes, while working with Python lists, we can have a problem in which we wish to find Even elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let us discuss certain ways to find in
8 min read
Python - Odd elements indices Sometimes, while working with Python lists, we can have a problem in which we wish to find Odd elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Letâs discuss certain way to find indic
7 min read
Python - Rear Kth elements Given a list, the task is to extract all Kth elements from rear end. Input : test_list = [3, 4, 5, 2, 1], K = 2 Output : [1, 5, 3] Explanation : Every 2nd elements are extracted from rear starting from 1. Input : test_list = [3, 4, 5], K = 1 Output : [5, 4, 3] Explanation : Every elements are extrac
3 min read
Python - K middle elements Given a List, extract K elements occurring in middle of list. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5, 9], K = 3 Output : [7, 8, 5] Explanation : Middle 3 elements are extracted. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5, 9], K = 7 Output : [3, 5, 7, 8, 5, 3, 5] Explanation : Middle 7 elements
5 min read
Separate Odd and Even Index Elements - Python We are given a list containing odd and even numbers we need to separate this in two different lists. For example, we are given a list a = [10, 20, 30, 40, 50, 60] we need to separate odd and even in two different list so that output becomes [10, 30, 50] [20, 40, 60].Using List SlicingWe can separate
3 min read