Open In App

Python program to check if any key has all the given list elements

Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionary with list values and a list, the task is to write a Python program to check if any key has all the list elements.

Examples:

Input : test_dict = {‘Gfg’ : [5, 3, 1, 6, 4], ‘is’ : [8, 2, 1, 6, 4], ‘best’ : [1, 2, 7, 3, 9], ‘for’ : [5, 2, 7, 8, 4, 1], ‘all’ : [8, 5, 3, 1, 2]}, find_list = [7, 9, 2]

Output : True

Explanation : best has all values, 7, 9, 2 hence 2 is returned.

Input : test_dict = {‘Gfg’ : [5, 3, 1, 6, 4], ‘is’ : [8, 2, 1, 6, 4], ‘best’ : [1, 2, 7, 3, 19], ‘for’ : [5, 2, 7, 8, 4, 1], ‘all’ : [8, 5, 3, 1, 2]}, find_list = [7, 9, 2]

Output : False

Explanation : No list has all values as find list.

Method #1 : Using issuperset() + loop

In this, we perform the task of finding the presence of all elements in the values list using issuperset(), and a loop is used to iterate all the keys.

Python3




# Python3 code to demonstrate working of
# Extract values of Particular Key in
# Nested Values Using list comprehension
 
# initializing dictionary
test_dict = {'Gfg': [5, 3, 1, 6, 4],
             'is': [8, 2, 1, 6, 4],
             'best': [1, 2, 7, 3, 9],
             'for': [5, 2, 7, 8, 4, 1],
             'all': [8, 5, 3, 1, 2]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
find_list = [7, 9, 2]
 
res = False
for key in test_dict:
 
    # checking if all values present using
    # superset
    if set(test_dict[key]).issuperset(find_list):
        res = True
 
# printing result
print("Is any value list superset ? : " + str(res))


Output:

The original dictionary is : {‘Gfg’: [5, 3, 1, 6, 4], ‘is’: [8, 2, 1, 6, 4], ‘best’: [1, 2, 7, 3, 9], ‘for’: [5, 2, 7, 8, 4, 1], ‘all’: [8, 5, 3, 1, 2]}

Is any value list superset ? : True

Time complexity: O(mn), where m is the number of keys in the dictionary and n is the length of the longest value list in the dictionary. 
Auxiliary space: O(n)

The auxiliary space complexity of the given code is O(n), where n is the length of the find_list. 

Method #2 : Using any() + issuperset()

In this, we perform the task of checking for all keys using any(). Rest all functions are similar to the above method.

Python3




# Python3 code to demonstrate working of
# Extract values of Particular Key in
# Nested Values Using any() + issuperset()
 
# initializing dictionary
test_dict = {'Gfg': [5, 3, 1, 6, 4],
             'is': [8, 2, 1, 6, 4],
             'best': [1, 2, 7, 3, 9],
             'for': [5, 2, 7, 8, 4, 1],
             'all': [8, 5, 3, 1, 2]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
find_list = [7, 9, 2]
 
res = any(set(sub).issuperset(find_list)
          for sub in test_dict.values())
 
# printing result
print("Is any value list superset ? : " + str(res))


Output:

The original dictionary is : {‘Gfg’: [5, 3, 1, 6, 4], ‘is’: [8, 2, 1, 6, 4], ‘best’: [1, 2, 7, 3, 9], ‘for’: [5, 2, 7, 8, 4, 1], ‘all’: [8, 5, 3, 1, 2]}

Is any value list superset ? : True

Method #3 : Without issuperset()

Python3




# Python3 code to demonstrate working of
# Extract values of Particular Key in
# Nested Values Using list comprehension
def contains(x,y):
    c=0
    for i in y:
        if i in x:
            c+=1
    if(c==len(y)):
        return True
    return False
# initializing dictionary
test_dict = {'Gfg': [5, 3, 1, 6, 4],
            'is': [8, 2, 1, 6, 4],
            'best': [1, 2, 7, 3, 9],
            'for': [5, 2, 7, 8, 4, 1],
            'all': [8, 5, 3, 1, 2]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
find_list = [7, 9, 2]
 
res = False
for i in test_dict.keys():
    if(contains(test_dict[i],find_list)):
        res=True
        break
# printing result
print("Is any value list superset ? : " + str(res))


Output

The original dictionary is : {'Gfg': [5, 3, 1, 6, 4], 'is': [8, 2, 1, 6, 4], 'best': [1, 2, 7, 3, 9], 'for': [5, 2, 7, 8, 4, 1], 'all': [8, 5, 3, 1, 2]}
Is any value list superset ? : True

Using Recursion:

Python3




def contains(x,y,res):
    if len(y) == 0:
        return res
    if y[0] in x:
        return contains(x,y[1:],True)
    else:
        return contains(x,y[1:],False)
     
def recursion(test_dict, find_list, res):
    if len(test_dict) == 0:
        return res
    key, value = test_dict.popitem()
    if contains(value, find_list,False):
        res = True
    return recursion(test_dict, find_list, res)
 
# initializing dictionary
test_dict = {'Gfg': [5, 3, 1, 6, 4],
            'is': [8, 2, 1, 6, 4],
            'best': [1, 2, 7, 3, 9],
            'for': [5, 2, 7, 8, 4, 1],
            'all': [8, 5, 3, 1, 2]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
find_list = [7, 9, 2]
 
res = recursion(test_dict, find_list, False)
# printing result
print("Is any value list superset ? : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original dictionary is : {'Gfg': [5, 3, 1, 6, 4], 'is': [8, 2, 1, 6, 4], 'best': [1, 2, 7, 3, 9], 'for': [5, 2, 7, 8, 4, 1], 'all': [8, 5, 3, 1, 2]}
Is any value list superset ? : True

Time Complexity:  O(N*M) where N is the number of keys in the dictionary, and M is the length of the find_list.

Auxiliary Space: O(n)

Method #5 : Using filter() and lambda functions: 

           This code demonstrates how to check if any of the values (lists) in a given dictionary are supersets of a target list.

The given dictionary “test_dict” contains keys (strings) and corresponding values (lists of integers). The target list “find_list” contains some integers that we want to find in any of the value lists of the dictionary.

The code first prints the original dictionary. It then applies the filter function to the dictionary values. The filter function takes a lambda function as an argument which checks if a given set of integers (obtained from the dictionary values) is a superset of the target list. The filter function returns a filter object which is then passed to the any function. The any function returns True if any of the elements in the filter object are True, meaning that at least one value list in the dictionary is a superset of the target list.

The code then prints the result indicating whether any value list in the dictionary is a superset of the target list or not.

Python3




test_dict = {'Gfg': [5, 3, 1, 6, 4],
             'is': [8, 2, 1, 6, 4],
             'best': [1, 2, 7, 3, 9],
             'for': [5, 2, 7, 8, 4, 1],
             'all': [8, 5, 3, 1, 2]}
find_list = [7, 9, 2]
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = any(filter(lambda x: set(x).issuperset(find_list), test_dict.values()))
print("Is any value list superset ? : " + str(res))
 
 
#This code is contributed by Jyothi pinjala.


Output

The original dictionary is : {'Gfg': [5, 3, 1, 6, 4], 'is': [8, 2, 1, 6, 4], 'best': [1, 2, 7, 3, 9], 'for': [5, 2, 7, 8, 4, 1], 'all': [8, 5, 3, 1, 2]}
Is any value list superset ? : True

Time Complexity: O(n*m)
Auxiliary Space: O(1)



Last Updated : 26 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads