Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Common list elements and dictionary values

Python – Common list elements and dictionary values

Given list and dictionary, extract common elements of List and Dictionary Values.

Input : test_list = [“Gfg”, “is”, “Best”, “For”], subs_dict = {4 : “Gfg”, 8 : “Geeks”, 9 : ” Good”, } 
Output : [‘Gfg’] 
Explanation : “Gfg” is common in both list and dictionary value.

Input : test_list = [“Gfg”, “is”, “Best”, “For”, “Geeks”], subs_dict = {4 : “Gfg”, 8 : “Geeks”, 9 : ” Best”, } 
Output : [‘Gfg’, “Geeks”, “Best”] 
Explanation : 3 common values are extracted.  

Method #1 : Using list comprehension + values()

 The combination of above functionalities provide a way in which this task can be performed in a single line. In this, we  extract values of dictionaries using values() and list comprehension is used to perform iteration and intersection checks.

Python3




# Python3 code to demonstrate working of
# List elements and dictionary values intersection
# Using list comprehension + values()
 
# initializing list
test_list = ["Gfg", "is", "Best", "For", "Geeks"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing subs. Dictionary
subs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", }
 
# Intersection of elements, using "in" for checking presence
res = [ele for ele in test_list if ele in subs_dict.values()]
         
# printing result
print("Intersection elements : " + str(res))


Output

The original list : ['Gfg', 'is', 'Best', 'For', 'Geeks']
Intersection elements : ['Gfg', 'Geeks']

Time Complexity: O(n), where n is the elements of dictionary
Auxiliary Space: O(n), where n is the size of dictionary

Method #2 :  Using set() + intersection()

In this approach both list and dictionary values are converted to set() and then intersection is performed to get common elements.

Python3




# Python3 code to demonstrate working of
# Common list elements and dictionary values
# Using set() and intersection()
 
# initializing list
test_list = ["Gfg", "is", "Best", "For", "Geeks"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing subs. Dictionary
subs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", }
 
# Intersection of elements, using set() to convert
# intersection() for common elements
res = list(set(test_list).intersection(list(subs_dict.values())))
         
# printing result
print("Intersection elements : " + str(res))


Output

The original list : ['Gfg', 'is', 'Best', 'For', 'Geeks']
Intersection elements : ['Geeks', 'Gfg']

Time Complexity: O(n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. 

Method 3: Using a for loop and a conditional statement

  1. Create a list test_list containing some string elements.
  2. Create a dictionary subs_dict containing integer keys and string values.
  3. Create an empty list res to store the intersection elements.
  4. Start a for loop to iterate through each element in test_list.
  5. Inside the for loop, check if the current element is in subs_dict.values().
  6. If the current element is found in subs_dict.values(), append it to the res list.
  7. After the for loop is complete, print the original list test_list.
  8. Finally, print the list of intersection elements stored in res.

Python3




# initializing list
test_list = ["Gfg", "is", "Best", "For", "Geeks"]
 
# initializing subs. Dictionary
subs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", }
 
# initializing result list
res = []
 
# loop through test_list and check if each element is in subs_dict.values()
for elem in test_list:
    if elem in subs_dict.values():
        res.append(elem)
 
# printing original list
print("The original list : " + str(test_list))
 
# printing result
print("Intersection elements : " + str(res))


Output

The original list : ['Gfg', 'is', 'Best', 'For', 'Geeks']
Intersection elements : ['Gfg', 'Geeks']

Time complexity: O(n).
Auxiliary space: O(n).

RELATED ARTICLES

Most Popular

Recent Comments