
- 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
Remove tuple from list of tuples if not containing any character in Python
When it is required to remove a tuple from a list of tuples based on a given condition, i.e the tuple doesn't contain a specific character, the list comprehension can be used.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
Below is a demonstration for the same −
Example
my_list = [('. ', 62), ('Mark', 5), ('Paul.', 21), ('.....', 0), ('-Jane', 115), ('Jake', 15), ('::', 63), ('John', 3), ('--', 1), ('there', 82), ('Harold', 100)] my_result = [(a, b) for a, b in my_list if any(c.isalpha() for c in a)] print("The tuple that doesn't contain any character has been removed") print("The resultant list of tuple is :") print(my_result)
Output
The tuple that doesn't contain any character has been removed The resultant list of tuple is : [('Mark', 5), ('Paul.', 21), ('-Jane', 115), ('Jake', 15), ('John', 3), ('there', 82), ('Harold', 100)]
Explanation
- A list of tuple is defined, and is displayed on the console.
- The list of tuple is iterated over, and checked whether it belongs to the alphabet family.
- This operation's data is stored in a variable.
- This variable is the output that is displayed on the console.
- Related Questions & Answers
- Remove tuples from list of tuples if greater than n in Python
- Remove duplicate tuples from list of tuples in Python
- Find the tuples containing the given element from a list of tuples in Python
- Python | Remove empty tuples from a list
- Remove tuples having duplicate first value from given list of tuples in Python
- Python – Check if any list element is present in Tuple
- Combinations of sum with tuples in tuple list in Python
- Reverse each tuple in a list of tuples in Python
- How can I subtract tuple of tuples from a tuple in Python?
- Check if element is present in tuple of tuples in Python
- Python Group by matching second tuple value in list of tuples
- Remove Tuples from the List having every element as None in Python
- Combining tuples in list of tuples in Python
- Create a list of tuples from given list having number and its cube in each tuple using Python
- Check if tuple has any None value in Python
Advertisements