Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Extracting keys not in values

Python – Extracting keys not in values

Sometimes, while working with Python dictionaries, we can have a problem in which we require to get all the keys that do not occur in values lists. This can have applications in the domains such as day-day programming. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using set() + values() + keys() + loop 

This is brute way to approach this task. In this, we test for elements in value lists, and keep adding them in separate list. Then we subtract this from extracted keys using keys(). 

Python3




# Python3 code to demonstrate working of
# Extracting keys not in values
# Using set() + keys() + values() + loop
 
# initializing Dictionary
test_dict = {3 : [1, 3, 4], 5 : [1, 2], 6 : [4, 3], 4 : [1, 3]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Extracting keys not in values
# Using set() + keys() + values() + loop
temp1 = set(test_dict.keys())
temp2 = set()
for ele in test_dict.values():
    temp2.update(ele)
res = list(temp1 - temp2)
 
# printing result
print("The extracted keys are : " + str(res))


Output : 

The original dictionary is : {3: [1, 3, 4], 4: [1, 3], 5: [1, 2], 6: [4, 3]} The extracted keys are : [5, 6]

Time Complexity: O(n*n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #2 : Using generator expression + set() 

This method is similar to above method. The difference is that it is performed as one liner way in compact format using generator expression. 

Python3




# Python3 code to demonstrate working of
# Extracting keys not in values
# Using generator expression + set()
 
# initializing Dictionary
test_dict = {3 : [1, 3, 4], 5 : [1, 2], 6 : [4, 3], 4 : [1, 3]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Extracting keys not in values
# Using generator expression + set()
res = list(set(test_dict) - set(ele for sub in test_dict.values() for ele in sub))
 
# printing result
print("The extracted keys are : " + str(res))


Output : 

The original dictionary is : {3: [1, 3, 4], 4: [1, 3], 5: [1, 2], 6: [4, 3]} The extracted keys are : [5, 6]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using filter()

In this method, we first create a set from the concatenated values of the dictionary. Then, we use the filter() method to extract the keys that are not in this set.

Python3




#Python3 code to demonstrate working of
#Extracting keys not in values
#Using filter()
#initializing Dictionary
test_dict = {3 : [1, 3, 4], 5 : [1, 2], 6 : [4, 3], 4 : [1, 3]}
 
#printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
#Extracting keys not in values
#Using filter()
temp = set([ele for sub in test_dict.values() for ele in sub])
res = list(filter(lambda x: x not in temp, test_dict.keys()))
 
#printing result
print("The extracted keys are : " + str(res))


Output

The original dictionary is : {3: [1, 3, 4], 5: [1, 2], 6: [4, 3], 4: [1, 3]}
The extracted keys are : [5, 6]

Time Complexity: O(n) where n is the number of keys in the dictionary.
Auxiliary Space: O(n) as we are using a set to store the elements of the values.

Method #4: Using list comprehension + set() + difference() method

Step-by-step approach:

  • Initialize the dictionary test_dict.
  • Use a list comprehension to create a set of all values in the dictionary.
  • Use the set() method to create a set of all keys in the dictionary.
  • Use the difference() method to find the difference between the set of keys and the set of values.
  • Convert the result back to a list and store it in the variable res.
  • Print the result.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Extracting keys not in values
# Using list comprehension + set() + difference() method
 
# initializing Dictionary
test_dict = {3 : [1, 3, 4], 5 : [1, 2], 6 : [4, 3], 4 : [1, 3]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Extracting keys not in values
# Using list comprehension + set() + difference() method
values_set = set([val for sublist in test_dict.values() for val in sublist])
keys_set = set(test_dict.keys())
res = list(keys_set.difference(values_set))
 
# printing result
print("The extracted keys are : " + str(res))


Output

The original dictionary is : {3: [1, 3, 4], 5: [1, 2], 6: [4, 3], 4: [1, 3]}
The extracted keys are : [5, 6]

Time complexity: O(n), where n is the total number of values in the dictionary.
Auxiliary space: O(n), where n is the total number of values in the dictionary.

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