Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython – Remove Disjoint Tuple keys from Dictionary

Python – Remove Disjoint Tuple keys from Dictionary

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 the element present keys from other lists. This kind of problem can have applications in data domains.

Input : test_dict = {('is', 9): 2, ('for', 8): 7}
rem_list = [9, 'is']
Output : {('for', 8): 7}

Ways to Remove Disjoint Tuple Keys from Dictionary

Remove Tuple Keys from Dictionary using Loop

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

Python3




# initializing dictionary
test_dict = {('Gfg', 3) : 4, ('is', 6) : 2,
             ('best', 10) : 3, ('for', 9) : 'neveropen'}
 
# 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): 'neveropen', ('is', 6): 2} 
Dictionary after removal : {('Gfg', 3): 4, ('best', 10): 3}

Remove Tuple Keys from Dictionary using Python Set isdisjoint()

The combination of the above functions can also be used to solve this problem. In this, we perform the task of comparison using isdisjoint()

Python3




# initializing dictionary
test_dict = {('Gfg', 3) : 4, ('is', 6) : 2,
             ('best', 10) : 3, ('for', 9) : 'neveropen'}
 
# 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): 'neveropen', ('is', 6): 2} 
Dictionary after removal : {('Gfg', 3): 4, ('best', 10): 3}

Remove Tuple Keys from Dictionary using Conditional Statement

This code uses dictionary comprehension to create a new dictionary that contains only the key-value pairs whose keys are not disjoint with the removal list. The condition if k[0] not in rem_list and k[1] not in rem_list checks that both elements of the key tuple are not in the removal list before adding the key-value pair to the new dictionary res.

Python3




# initializing dictionary
test_dict = {('Gfg', 3) : 4, ('is', 6) : 2,
             ('best', 10) : 3, ('for', 9) : 'neveropen'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing List
rem_list = [9, 'is']
 
# Remove Disjoint Tuple keys from Dictionary
# Using dictionary comprehension
res = {k:v for k,v in test_dict.items() if k[0] not in rem_list and
                           k[1] not in rem_list}
 
# printing result
print("Dictionary after removal : " + str(res))


Output

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




Remove Tuple Keys from Dictionary using list comprehension and dict() function

Initialize the dictionary. Initialize the list containing the keys to be removed. Use list comprehension to create a list of keys that are not in the remove list. Create a new dictionary by passing the list of keys and the original dictionary to the dict() function. Print the original Dictionary and the filtered dictionary.

Python3




# initializing dictionary
test_dict = {('Gfg', 3) : 4, ('is', 6) : 2,
             ('best', 10) : 3, ('for', 9) : 'neveropen'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing List
rem_list = [9, 'is']
 
# using list comprehension to create list of keys not in remove list
keys_list = [key for key in test_dict.
             keys() if key[0] not in rem_list and
             key[1] not in rem_list]
 
# creating new dictionary with filtered keys
res_dict = dict((key, test_dict[key]) for key in keys_list)
 
# printing result
print("Dictionary after removal : " + str(res_dict))


Output

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




Remove Tuple Keys from Dictionary using filter() and lambda function

As previously, initialize the dictionary and the list to remove. Use the filter() function to eliminate keys that are disjunct from the list. In this section, we’ll define a lambda function that checks to see if the first and second items of the key tuple aren’t in the delete list. Create a new dictionary with the same values as the old dictionary using the filtered keys. Print the outcome.

Python3




# initializing dictionary
test_dict = {('Gfg', 3) : 4, ('is', 6) : 2,
             ('best', 10) : 3, ('for', 9) : 'neveropen'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing List
rem_list = [9, 'is']
 
# using filter() function to filter out disjoint keys
filtered_keys = filter(lambda key:
                       key[0] not in rem_list and key[1] not in
                       rem_list, test_dict.keys())
 
# creating new dictionary with filtered keys
res_dict = {key: test_dict[key] for key in filtered_keys}
 
# printing result
print("Dictionary after removal : " + str(res_dict))


Output

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




Remove Tuple Keys from Dictionary using map() 

Initialize a dictionary called test_dict with 4 key-value pairs. Print the original dictionary using print(). Initialize a list called rem_list with 2 values. Use the map() function to create a generator that iterates over the dictionary keys and returns True for keys that are not in the dictionary or whose first and second elements are not in rem_list. Create a new dictionary called res_dict using a dictionary comprehension that iterates over the original dictionary keys and values, and only includes keys for which the corresponding value in the filtered_keys generator is True. Print the resulting dictionary using print().

Python3




# initializing dictionary
test_dict = {('Gfg', 3) : 4, ('is', 6) : 2,
             ('best', 10) : 3, ('for', 9) : 'neveropen'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing List
rem_list = [9, 'is']
 
# using map() to filter out disjoint keys
filtered_keys = map(lambda key: key not in test_dict.keys() or
                    key[0] not in rem_list and key[1] not in
                    rem_list, test_dict.copy())
 
# creating new dictionary with filtered keys
res_dict = {key: test_dict[key] for key in test_dict.
            keys() if filtered_keys.__next__()}
 
# printing result
print("Dictionary after removal : " + str(res_dict))


Output

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




Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments