- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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.
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.
- Related Articles
- 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
- Python – Remove Rows for similar Kth column element
- 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
- Closest Pair to Kth index element in Tuple using Python
- Python – Extract Kth element of every Nth tuple in List
- C++ Program to Find kth Largest Element in a Sequence
- Kth Largest Element in an Array
- Swap kth element of array - JavaScript
- Find maximum sum taking every Kth element in the array in C++
- Kth smallest element after every insertion in C++

Advertisements