Sometimes, while working with Python records, we can have a problem in which we need to check for paired existance inside the record, or else if one doesn’t exist, other also should not. This kind of problem is common in domains such as Data Science and web development. Let’s discuss certain ways in which this task can be performed.
test_list = [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
pairs = (‘is’, ‘good’)
Output : [(‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
test_list = [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘better’)]
pairs = (‘better’, ‘good’)
Output : 
Method #1 : Using generator expression
This is brute force way in which this task can be performed. In this, we check for existance/non-existance of both numbers and accept the result if, none or both are present.
The original list is : [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
The resultant records : [(‘Gfg’, ‘is’, ‘Best’), (‘CS’, ‘is’, ‘good’)]
Method #2 : Using XNOR
This is yet another way to solve this problem. In this, use the power of XOR operator to perform this task and negate the result.
The original list is : [('Gfg', 'is', 'Best'), ('Gfg', 'is', 'good'), ('CS', 'is', 'good')] The resultant records : [('Gfg', 'is', 'Best'), ('CS', 'is', 'good')]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Paired Neighbors to Adjacency Dictionary
- Python - Paired elements grouping
- Python | Retain records of specific length
- Python | Convert tuple records to single string
- Python | Intersection in Tuple Records Data
- Python | Get Top N elements from Records
- Python | Cummulative Columns summation of Records
- Python | Group and count similar records
- Python | Minimum K records of Nth index in tuple list
- Python - Find minimum k records from tuple list
- Python - Rear element extraction from list of tuples records
- Python | Maximize Column in Records List
- Python - Cumulative Records Product
- Python - Remove nested records from tuple
- Python - Remove all duplicate occurring tuple records
- Python | Records Intersection
- Python | Concatenate All Records
- Python | Incremental Records Product
- Python - Records with Value at K index
- Python - Records Maxima in List of Tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.