- 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 – Filter rows with Elements as Multiple of K
When it is required to filter rows with elements which are multiples of K, a list comprehension and modulus operator are used.
Below is a demonstration of the same −
my_list = [[15, 10, 25], [14, 28, 23], [120, 55], [55, 30, 203]] print("The list is :") print(my_list) K = 5 print("The value of K is ") print(K) my_result = [index for index in my_list if all(element % K == 0 for element in index)] print("The result is :") print(my_result)
The list is : [[15, 10, 25], [14, 28, 23], [120, 55], [55, 30, 203]] The value of K is 5 The result is : [[15, 10, 25], [120, 55]]
A list of list is defined and displayed on the console.
The value for ‘K’ is defined and displayed on the console.
A list comprehension is used to iterate over the list, and modulus of each element with K is compared to 0.
The ‘all’ operator is used to check the output based on all elements.
If the value is ‘True’, this is assigned to a variable.
This is the output that is displayed on the console.
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Filter Sorted Rows
- Python – Filter rows with only Alphabets from List of Lists
- Filter the rows – Python Pandas
- Python – Filter tuple with all same elements
- Python – Rows with K string in Matrix
- Python - Sort rows by Frequency of K
- Python Program to Filter Rows with a specific Pair Sum
- Python – Rows with all List elements
- Python – Filter rows without Space Strings
- Python – Filter consecutive elements Tuples
- Delete elements with frequency atmost K in Python
- Python – Filter Tuples Product greater than K
- Python - Filter Rows Based on Column Values with query function in Pandas?