- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Find Kth Even Element
When it is required to find the ‘K’th even element in a list, a list comprehension and the ‘%’ operator is used.
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)
The list is : [14, 63, 28, 32, 18, 99, 13, 61] The value of K is : 3 The result is : 18
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.
- Python – Remove Rows for similar Kth column element
- Python – Extract Kth element of every Nth tuple in List
- Python – Cross Join every Kth segment
- Program to find kth smallest element in linear time in Python
- Kth Largest Element in an Array in Python
- Kth Smallest Element in a BST in Python
- Kth Largest Element in a Stream in Python
- C++ Program to Find kth Largest Element in a Sequence
- Kth Smallest Element in a Sorted Matrix in Python
- Filter Tuples by Kth element from List in Python
- Program to find the kth smallest element in a Binary Search Tree in Python
- Kth Largest Element in an Array
- Closest Pair to Kth index element in Tuple using Python
- Python – Extract rows with Even length strings