- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 matching tuples in Python
When it is required to remove the matching tuples from two list of tuples, 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 −
my_list_1 = [('Hi', 'there'), ('Jane', 'Hi'), ('how', 'are'), ('you', '!')] my_list_2 = [('Hi', 'there'), ('Hi', 'Jane')] print("The first list is : ") print(my_list_1) print("The second list is : ") print(my_list_2) my_result = [sub for sub in my_list_1 if sub not in my_list_2] print("The filtered out list of tuples is : ") print(my_result)
The first list is : [('Hi', 'there'), ('Jane', 'Hi'), ('how', 'are'), ('you', '!')] The second list is : [('Hi', 'there'), ('Hi', 'Jane')] The filtered out list of tuples is : [('Jane', 'Hi'), ('how', 'are'), ('you', '!')]
- Two list of tuples are defined, and are displayed on the console.
- A list comprehension is used to iterate through the tuples.
- This will filter out tuples that are present in both the list of tuples.
- The ones left out are displayed on the console.
- Remove duplicate tuples from list of tuples in Python
- Remove Tuples of Length K in Python
- Python – Remove Dictionaries with Matching Values
- Python Group by matching second tuple value in list of tuples
- Remove tuples from list of tuples if greater than n in Python
- Python | Remove empty tuples from a list
- Remove tuples having duplicate first value from given list of tuples in Python
- Remove duplicate lists in tuples (Preserving Order) in Python
- Python – Remove Tuples with difference greater than K
- Wildcard Matching in Python
- Combining tuples in list of tuples in Python
- Remove Tuples from the List having every element as None in Python
- Updating Tuples in Python
- Compare tuples in Python
- Count tuples occurrence in list of tuples in Python