

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 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 Questions & Answers
- Python – Filter Tuples with Integers
- Python – Filter all uppercase characters from given list of tuples
- Python - Filter Supersequence Strings
- Python – Filter unique valued tuples
- Python – Filter consecutive elements Tuples
- Python – Filter Tuples Product greater than K
- Python – Filter Similar Case Strings
- Python – Strings with all given List characters
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Sort list of tuples by specific ordering in Python
- 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
- Convert list of strings and characters to list of characters in Python
Advertisements