Related Articles

# Python – Remove Disjoint Tuple keys from Dictionary

• Last Updated : 04 May, 2020

Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove tuple keys from records on the basis of key presence matching any one of element present key from other lists. This kind of problem can have applications in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {(9, 3) : 4, (‘is’, 6) : 2, (‘for’, 9) : ‘geeks’}
Output : {}

Input : test_dict = {(‘is’, 9): 2, (‘for’, 8): 7}
Output : {(‘for’, 8): 7}

Method #1 : Using loop
This is one of the ways in which this task can be performed. In this we perform an iteration of dictionary key and check for the presence of any key in required and perform removal accordingly.

 `# Python3 code to demonstrate working of ``# Remove Disjoint Tuple keys from Dictionary``# Using loop`` ` `# initializing dictionary``test_dict ``=` `{(``'Gfg'``, ``3``) : ``4``, (``'is'``, ``6``) : ``2``, (``'best'``, ``10``) : ``3``, (``'for'``, ``9``) : ``'geeks'``}`` ` `# printing original dictionary``print``(``"The original dictionary : "` `+` `str``(test_dict))`` ` `# initializing List ``rem_list ``=` `[``9``, ``'is'``]`` ` `# Remove Disjoint Tuple keys from Dictionary``# Using loop``res ``=` `dict``()``for` `idx ``in` `test_dict:``    ``if` `idx[``0``] ``not` `in` `rem_list ``and` `idx[``1``] ``not` `in` `rem_list:``        ``res[idx] ``=` `test_dict[idx]`` ` `# printing result ``print``(``"Dictionary after removal : "` `+` `str``(res)) `
Output :

The original dictionary : {(‘Gfg’, 3): 4, (‘best’, 10): 3, (‘for’, 9): ‘geeks’, (‘is’, 6): 2}
Dictionary after removal : {(‘Gfg’, 3): 4, (‘best’, 10): 3}

Method #2 : Using `set() + dictionary comprehension + isdisjoint()`
The combination of above functions can also be used to solve this problem. In this, we perform the task of comparison using isdisjoint().

 `# Python3 code to demonstrate working of ``# Remove Disjoint Tuple keys from Dictionary``# Using set() + dictionary comprehension + isdisjoint()`` ` `# initializing dictionary``test_dict ``=` `{(``'Gfg'``, ``3``) : ``4``, (``'is'``, ``6``) : ``2``, (``'best'``, ``10``) : ``3``, (``'for'``, ``9``) : ``'geeks'``}`` ` `# printing original dictionary``print``(``"The original dictionary : "` `+` `str``(test_dict))`` ` `# initializing List ``rem_list ``=` `[``9``, ``'is'``]`` ` `# Remove Disjoint Tuple keys from Dictionary``# Using set() + dictionary comprehension + isdisjoint()``res ``=` `{keys: val ``for` `keys, val ``in` `test_dict.items() ``if` `set``(keys).isdisjoint(rem_list)}`` ` `# printing result ``print``(``"Dictionary after removal : "` `+` `str``(res)) `
Output :

The original dictionary : {(‘Gfg’, 3): 4, (‘best’, 10): 3, (‘for’, 9): ‘geeks’, (‘is’, 6): 2}
Dictionary after removal : {(‘Gfg’, 3): 4, (‘best’, 10): 3}

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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up