Python – Paired Existance in Records

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.

Input :
test_list = [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
pairs = (‘is’, ‘good’)
Output : [(‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]

Input :
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Paired Existance in Records
# Using generator expression
  
# initializing list
test_list = [('Gfg', 'is', 'Best'),
             ('Gfg', 'is', 'good'), 
             ('CS', 'is', 'good')]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Pairs 
pairs = ('Gfg', 'Best')
  
# Paired Existance in Records
# Using generator expression
res = []
for sub in test_list:
    if ((pairs[0] in sub and pairs[1] in sub) or (
         pairs[0] not in sub and pairs[1] not in sub)):
        res.append(sub)
  
# printing result 
print("The resultant records : " + str(res)) 

chevron_right


Output :



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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Paired Existance in Records
# Using XNOR
  
# initializing list
test_list = [('Gfg', 'is', 'Best'),
             ('Gfg', 'is', 'good'), 
             ('CS', 'is', 'good')]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Pairs 
pairs = ('Gfg', 'Best')
  
# Paired Existance in Records
# Using XNOR
res = []
for sub in test_list:
    if (not ((pairs[0] in sub) ^ (pairs[1] in sub))):
        res.append(sub)
  
# printing result 
print("The resultant records : " + str(res)) 

chevron_right


Output :

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.