
- 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
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 Articles
- 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
- Reverse each tuple in a list of tuples in Python
- Combinations of sum with tuples in tuple list in Python
- Python – Check if any list element is present in Tuple
- How can I subtract tuple of tuples from a tuple 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
- Python – Remove Tuples from a List having every element as None
- Create a list of tuples from given list having number and its cube in each tuple using Python
- Check if element is present in tuple of tuples in Python
- Python – Remove dictionary from a list of dictionaries if a particular value is not present

Advertisements