- 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 Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- 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