
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
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)
Output
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]]
Explanation
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.
- Related Articles
- 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
- Python – Filter tuple with all same elements
- Filter the rows – Python Pandas
- Python – Rows with K string in Matrix
- Python – Rows with all List elements
- Python Program to Filter Rows with a specific Pair Sum
- Python – Filter rows without Space Strings
- Filter Pandas Dataframe with multiple conditions
- Python – Filter consecutive elements Tuples
- Python - Filter Rows Based on Column Values with query function in Pandas?
- Python - Sort rows by Frequency of K
- Python – Filter Tuples Product greater than K

Advertisements