- 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 â€“ Extract rows with Even length strings
When it is required to extract rows with even length strings, a list comprehension along with the ‘all’ operator and ‘%’ operator is used.
Below is a demonstration of the same −
my_list = [["python", "is", "best"], ["best", "good", "python"], ["is", "better"], ["for", "coders"]] print("The list is :") print(my_list) my_result = [row for row in my_list if all(len(element ) % 2 == 0 for element in row)] print("The resultant list is :") print(my_result)
The list is : [['python', 'is', 'best'], ['best', 'good', 'python'], ['is', 'better'], ['for', 'coders']] The resultant list is : [['python', 'is', 'best'], ['best', 'good', 'python'], ['is', 'better']]
A list of list with strings is defined and is displayed on the console.
A list comprehension is used to iterate over the elements of the list.
It checks to see if the elements have even length using the ‘all’ operator and modulus operator.
If yes, it is stored in a list, and assigned to a variable.
This variable is displayed as output on the console.
- Related Articles
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python â€“ Extract rows with Complex data types
- Python â€“ Extract Sorted Strings
- Python Program to Extract Strings with a digit
- Python â€“ Extract Paired Rows
- Python â€“ Extract Strings with Successive Alphabets in Alphabetical Order
- Python program to extract rows with common difference elements
- Python â€“ Extract Particular data type rows
- Python â€“ Filter rows without Space Strings
- Extract numbers from list of strings in Python
- Program to find length of longest path with even sum in Python
- Python Program to Extract Strings with at least given number of characters from other list
- How to extract required data from structured strings in Python?
- Python program to omit K length Rows
- Program to find length of longest substring with even vowel counts in Python