Python Program to Check if any Key has all the given List Elements

In Python, we often need to check if any key in a dictionary contains all elements from a given list. This is useful for data validation, filtering, and search operations.

Let's explore different approaches to solve this problem using various Python techniques.

Problem Overview

Given a dictionary with lists as values and a target list, we need to determine if any dictionary value contains all elements from the target list.

Example

Consider this dictionary and target list ?

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]
print("Input dictionary:", inputDict)
print("Target list:", inputList)
Input dictionary: {'hello': [4, 2, 8, 1], 'tutorialspoint': [5, 12, 10, 6], 'python': [9, 3, 7, 1], 'users': [3, 6, 1, 8, 2]}
Target list: [1, 2, 3]

The 'users' key contains all elements [1, 2, 3] from our target list, so the result should be True.

Method 1: Using for Loop with issuperset()

The issuperset() function checks if a set contains all elements from another iterable ?

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]

result = False

for key in inputDict:
    if set(inputDict[key]).issuperset(inputList):
        result = True
        break

print("Do any key values contain all target elements?", result)
Do any key values contain all target elements? True

Method 2: Using any() with issuperset()

The any() function returns True if any element in an iterable is True. This provides a more concise solution ?

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]

result = any(set(value).issuperset(inputList) for value in inputDict.values())

print("Do any key values contain all target elements?", result)
Do any key values contain all target elements? True

Method 3: Manual Check Without issuperset()

This approach manually counts matching elements ?

def contains_all_elements(dict_value, target_list):
    count = 0
    for element in target_list:
        if element in dict_value:
            count += 1
    return count == len(target_list)

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]

result = False
for key in inputDict:
    if contains_all_elements(inputDict[key], inputList):
        result = True
        break

print("Do any key values contain all target elements?", result)
Do any key values contain all target elements? True

Method 4: Using Set Operations

This method uses set intersection to check if all target elements are present ?

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]

def check_with_sets(dict_value, target_list):
    return set(target_list).issubset(set(dict_value))

result = any(check_with_sets(value, inputList) for value in inputDict.values())

print("Do any key values contain all target elements?", result)
Do any key values contain all target elements? True

Performance Comparison

Method Time Complexity Best For
for loop + issuperset() O(n*m) Simple, readable code
any() + issuperset() O(n*m) Concise, Pythonic
Manual counting O(n*m) No set conversion overhead
Set operations O(n*m) Clear mathematical logic

Conclusion

The any() + issuperset() method provides the most Pythonic and concise solution. Use manual counting when avoiding set operations, or set operations for clearer mathematical logic. All methods have similar time complexity but differ in readability and memory usage.

Updated on: 2026-03-27T12:52:40+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements