
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Any Key Has All Given List Elements in Python
An array(list) of comma-separated values (items) enclosed in square brackets is one of the most flexible data types available in Python. The fact that a list's elements do not have to be of the same type is important.
In this article, we will learn a python program to check if any key has all the given list elements.
Example
Assume we have taken an input dictionary and a list. We will now check whether the values of any key of the input dictionary contain given list elements using the above methods.
Input
inputDict = {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} inputList = [1, 2, 3]
Output
Do the values of any key have given list elements?: True
In the above dictionary, only the 'users' key contains the values of all the input list elements 1, 2, 3. Hence the result is True.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task
Create a variable to store the input dictionary and print it.
Create another variable to store the input list.
Initialize the result as False.
Use the for loop to traverse through the keys of the input dictionary.
Use the if conditional statement to check whether the corresponding value of the current key is present in the input list using issuperset() function.
Update the result as True if the condition is true.
Print the result.
Example 1: Using for loop and issuperset() function
issuperset() function(returns True if all elements in the given set are present in the original set; else, returns False)
The following program checks whether the values of any key of the input dictionary contain given list elements using the for loop and issuperset() function -
# input dictionary inputDict = {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} # printing input dictionary print("Input dictionary:\n", inputDict) # input list inputList = [1, 2, 3] # initializing result as False result = False # traversing through the keys of the input dictionary for k in inputDict: # checking whether the corresponding value of the current key is # present in the input list using issuperset() function if set(inputDict[k]).issuperset(inputList): # updating the result as True if the condition is true result = True # printing the result print("Do the values of any key have given list elements?:", result)
Output
Input dictionary: {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} Do the values of any key have given list elements?: True
Example 2: Using any() and issuperset() functions
any() function: returns True if any item in an iterable is true, else returns False.
The following program checks whether the values of any key of the input dictionary contain given list elements using the any() and issuperset() functions -
# input dictionary inputDict = {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} # printing input dictionary print("Input dictionary:\n", inputDict) # input list inputList = [1, 2, 3] # Checking if the key has all list elements result = any(set(val).issuperset(inputList) for val in inputDict.values()) # printing the result print("Do the values of any key have given list elements?:", result)
Output
Input dictionary: {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} Do the values of any key have given list elements?: True
Example 3: Without issuperset() function
The following program checks whether the values of any key of the input dictionary contain given list elements without using issuperset() function
# creating a function to check key contains all list elements # by accepting dictionary value, input list as arguments def containsListElements(a,b): cnt =0 for k in b: if k in a: cnt+=1 if(cnt==len(b)): return True return False # input dictionary inputDict = {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} # input list inputList = [1, 2, 3] # initializing result as False result = False # traversing through the keys of input dictionary for k in inputDict.keys(): # calling the above defined contains() function if(containsListElements(inputDict[k],inputList)): # updating the result as True if the condition is true result=True # breaking the loop break # printing the result print("Do the values of any key have given list elements?:", result)
Output
Do the values of any key have given list elements?: True
Example 4: Using Recursion
The following program checks whether the values of any key of the input dictionary contain given list elements using recursion
def containsListElements(a, b, result): if len(b) == 0: return result if b[0] in a: return containsListElements(a, b[1:], True) else: return containsListElements(a, b[1:], False) def recursiveFunc(inputDict, inputList, result): # returning the result directly if the length of the input dictionary is 0 if len(inputDict) == 0: return result # removing the key value from the given dictionary key, val = inputDict.popitem() if containsListElements(val, inputList, False): # updating the result as True if the condition is true result = True # recursive logic return recursiveFunc(inputDict, inputList, result) # input dictionary inputDict = {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]} # input list inputList = [1, 2, 3] result = recursiveFunc(inputDict, inputList, False) # printing the result print("Do the values of any key have given list elements?:", result)
Output
Do the values of any key have given list elements?: True
Conclusion
In this article, we have learned 4 different methods to check if any key has all the given list elements. Additionally, we learned how to use the pop() function to delete the key-value pairs from the dictionary.