Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Retain list elements value items

  • Last Updated : 02 Jun, 2020

Sometimes, while working with Python dictionaries, we can have a problem in which we need to retain only those keys, whose values are part of target list. This kind of problem can have potential problem in many domains including web development. Let’s discuss certain ways in which this task can be performed.

Input :
test_dict = {‘gfg’: 3}
tar_list = [3, 4, 10] (Values to retain)
Output : {‘gfg’: 3}

Input :
test_dict = {‘gfg’: 5, ‘best’: 12}
tar_list = [3, 4, 10] (Values to retain)
Output : {}

Method #1: Using dictionary comprehension
This is one of the ways in which this problem can be solved. In this, we perform the task of filtering using conditional expression inside comprehension, retaining only those items present list.




# Python3 code to demonstrate working of 
# Retain list elements value items
# Using dictionary comprehension
  
# initializing dictionary
test_dict = {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'geeks': 10}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing target list 
tar_list = [3, 4, 10]
  
# Retain list elements value items
# Using dictionary comprehension
res = {key : val for key, val in test_dict.items() if val in tar_list}
      
# printing result 
print("The filtered dictionary : " + str(res)) 
Output :
The original dictionary is : {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'geeks': 10}
The filtered dictionary : {'gfg': 3, 'best': 4, 'geeks': 10}

 

Method #2 : Using filter() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of filteration using filter, logic fed using lambda function.




# Python3 code to demonstrate working of 
# Retain list elements value items
# Using filter() + lambda
  
# initializing dictionary
test_dict = {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'geeks': 10}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing target list 
tar_list = [3, 4, 10]
  
# Retain list elements value items
# Using filter() + lambda
res = dict(filter(lambda sub: sub[1] in tar_list, test_dict.items()))
      
# printing result 
print("The filtered dictionary : " + str(res)) 
Output :
The original dictionary is : {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'geeks': 10}
The filtered dictionary : {'gfg': 3, 'best': 4, 'geeks': 10}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!