- 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 Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- Related Articles
- Remove duplicate tuples from list of tuples in Python
- Python – Remove Dictionaries with Matching Values
- Remove Tuples of Length K in Python
- Python Group by matching second tuple value in list of tuples
- Remove tuples from list of tuples if greater than n in Python
- Remove tuples having duplicate first value from given list of tuples in Python
- Python | Remove empty tuples from a list
- Remove duplicate lists in tuples (Preserving Order) in Python
- Python – Remove Tuples with difference greater than K
- Remove Tuples from the List having every element as None in Python
- Python – Remove Tuples from a List having every element as None
- Remove tuple from list of tuples if not containing any character in Python
- Combining tuples in list of tuples in Python
- Wildcard Matching in Python
- Count tuples occurrence in list of tuples in Python