Python program to check if any key has all the given list elements
Last Updated :
26 Feb, 2023
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
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 ]}
print ( "The original dictionary is : " + str (test_dict))
find_list = [ 7 , 9 , 2 ]
res = False
for key in test_dict:
if set (test_dict[key]).issuperset(find_list):
res = True
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
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 ]}
print ( "The original dictionary is : " + str (test_dict))
find_list = [ 7 , 9 , 2 ]
res = any ( set (sub).issuperset(find_list)
for sub in test_dict.values())
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
def contains(x,y):
c = 0
for i in y:
if i in x:
c + = 1
if (c = = len (y)):
return True
return False
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 ]}
print ( "The original dictionary is : " + str (test_dict))
find_list = [ 7 , 9 , 2 ]
res = False
for i in test_dict.keys():
if (contains(test_dict[i],find_list)):
res = True
break
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)
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 ]}
print ( "The original dictionary is : " + str (test_dict))
find_list = [ 7 , 9 , 2 ]
res = recursion(test_dict, find_list, False )
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(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 ]
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))
|
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)
Similar Reads
Python | Check if any element occurs n times in given list
Given a list, the task is to find whether any element occurs 'n' times in given list of integers. It will basically check for the first element that occurs n number of times. Examples: Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2], n = 3 Output : 2 Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2, 1, 1], n = 4
5 min read
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the fir
5 min read
Python Program to check whether all elements in a string list are numeric
Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False. Input : test_list = ["434", "823", "98", "74"] Output : True Explanation : All Strings are digits.Input : tes
5 min read
Python - Check if any list element is present in Tuple
Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements.
6 min read
Python - Check if all elements in List are same
To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. [GFGTABS] Python a = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) [/GFGTABS]OutputTrue Ex
3 min read
Test if all elements are present in list-Python
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.
3 min read
Python program to check if a given string is Keyword or not
This article will explore how to check if a given string is a reserved keyword in Python. Using the keyword We can easily determine whether a string conflicts with Python's built-in syntax rules. Using iskeyword()The keyword module in Python provides a function iskeyword() to check if a string is a
2 min read
Python | Check if two lists have any element in common
Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check. Using set IntersectionSet intersection uses Python's
3 min read
Python - Check if tuple list has all K
Sometimes, while working with Python records, we can have a problem in which we need to test if all the elements in tuples of tuple list are K. This problem can have applications in many data domains such as Machine Learning and Web development. Let's discuss certain ways in which this task can be p
7 min read
Python - Check if two lists have at-least one element common
Checking if two lists share at least one common element is a frequent task when working with datasets, filtering, or validating data. Python offers multiple efficient ways to solve this depending on the size and structure of the lists. Using set IntersectionConverting both lists into sets and findin
3 min read