The provided Python code extracts key-value pairs from a dictionary...

July 1, 2025 at 04:32 PM

# Python3 code to demonstrate working of # Extract dictionary items with List elements # Using all() + dictionary comprehension # initializing dictionary test_dict = {'gfg' : [4, 6], 'is' : [10], 'best' : [4, 5, 7]} # printing original dictionary print("The original dictionary : " + str(test_dict)) # initializing req_list req_list = [4, 6, 10] # Extract dictionary items with List elements # Using all() + dictionary comprehension res = {key: val for key, val in test_dict.items() if not any(vals in req_list for vals in val)} # printing result print("The extracted dictionary: " + str(res))

The provided Python code extracts key-value pairs from a dictionary where the values (which are lists) do not have any elements in common with a given list (req_list). Here is a detailed explanation of what the code does:

Code Breakdown:

  1. Dictionary Initialization:

    test_dict = {'gfg': [4, 6], 'is': [10], 'best': [4, 5, 7]}
    

    The dictionary test_dict maps keys ('gfg', 'is', 'best') to lists of integers.

  2. Print Original Dictionary:

    print("The original dictionary : " + str(test_dict))
    

    This prints the original dictionary, but note that here " is an HTML encoded version of a quotation mark ("). It should be replaced with proper Python quotes like so:

    print("The original dictionary : " + str(test_dict))
    
  3. Define the Required List:

    req_list = [4, 6, 10]
    

    A list of integers (req_list) is defined. The goal is to exclude entries from test_dict if any of the integers in their associated value (list) are present in req_list.

  4. Extract Dictionary Items Without Elements in req_list:

    res = {key: val for key, val in test_dict.items() if not any(vals in req_list for vals in val)}
    

    This uses dictionary comprehension to filter out items from test_dict.

    • test_dict.items() is used to iterate over the key-value pairs of the dictionary.
    • For each key-value pair (key, val), the if not any() condition is evaluated:
      • vals in req_list for vals in val checks if any element (vals) in the list val is present in req_list.
      • If none of the elements in val are found in req_list, the key-value pair is included in the resulting dictionary (res).
  5. Print the Result:

    print("The extracted dictionary: " + str(res))
    

    Once again, replace " with proper quotation marks:

    print("The extracted dictionary: " + str(res))
    

Output:

Given the input dictionary:

test_dict = {'gfg': [4, 6], 'is': [10], 'best': [4, 5, 7]}

And req_list = [4, 6, 10], the output would be:

  • Key 'gfg' is excluded because [4, 6] has elements in req_list.
  • Key 'is' is excluded because [10] has elements in req_list.
  • Key 'best' is excluded because [4, 5, 7] has elements in req_list.

Thus, the resulting dictionary is:

The extracted dictionary: {}

Summary:

This code extracts key-value pairs from a dictionary where the values (lists) do not share any elements with a specified list (req_list). If all values have overlapping elements with req_list, the result will be an empty dictionary, as in the given example.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node