
- 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 Tuples with Strings of specific characters
When it is required to filter tuples with strings that have specific characters, a list comprehension and the ‘all’ operator is used.
Example
Below is a demonstration of the same −
my_list = [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] print("The list is :") print(my_list) char_string = 'pyestb' my_result = [index for index in my_list if all(all(sub in char_string for sub in element) for element in index)] print("The result is : ") print(my_result)
Output
The list is : [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] The result is : [('pyt', 'best')]
Explanation
A list of tuple is defined and displayed on the console.
A string is defined.
A list comprehension is used to iterate over the list and ‘all’ operator is used on elements to check if that specific string is present in any elements of the list.
This is converted to a list and is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python β Filter all uppercase characters from given list of tuples
- Python β Filter Tuples with Integers
- Python β Filter unique valued tuples
- Python β Filter consecutive elements Tuples
- Python - Filter Supersequence Strings
- Python β Strings with all given List characters
- Python β Filter Tuples Product greater than K
- Python β Filter Similar Case Strings
- Python Program β Strings with all given List characters
- Python β Filter Strings within ASCII range
- Python β Filter rows without Space Strings
- Python Program to Filter Rows with a specific Pair Sum
- Filter Tuples by Kth element from List in Python
- Filter tuples according to list element presence in Python
- Sort list of tuples by specific ordering in Python

Advertisements