Change language

Python | Filter dictionary key based on values ​​in selectable list

| |

Method # 1: Using List Comprehension
List Comprehension can be used to solve this particular problem. It’s just a shorthand way of doing this instead of writing a loop.

# Python3 demo code
# getting selective dictionary keys
# using comprehension list

 
# initializing dictionary

test_dict = { "Akash" : 1 , "Akshat" : 2 , " Nikhil " : 3 , "Manjeet" : 4 }

 
# initialize select list keys

select_list = [ ’Manjeet’ , ’Nikhil’ ]

  
# printing the original dictionary

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

 
# print selection list

print ( " The selective list is: " + str (select_list ))

 
# using comprehension list
# getting selective dictionary keys

res = [test_dict [i] for i in select_list if i in test_dict]

  
# print result

print ( " The selected values ​​from list keys is: " +   str (res))

Exit :

The original dictionary is: {’Nikhil’: 3, ’Akshat’: 2, ’Manjeet’: 4, ’Akash’: 1}
The selective list is: [’Manjeet’, ’Nikhil’]
The selected values ​​from list keys is: [4, 3]

Method # 2: Using set.intersection()
This is the most elegant method in which this task can be accomplished. The set intersection property can give generic keys that can be extracted and then values ​​can be computed.

# Python3 demo code
# getting selective dictionary keys
# using set.intersection ()

 
# initializing dictionary

test_dict = { "Akash" : 1 , "Akshat" : 2 , " Nikhil " : 3 , "Manjeet" : 4 }

 
# Selective list keys initialization

select_list = [ ’Manjeet’ , ’ Nikhil’ ]

 
# print original dictionary

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

 
# print selection list

print < / code> ( "The selective list is:" + str (select_list))

 
# using set.intersection ()
# getting selective dictionary keys

temp = list ( set (select_list) .intersection (test_dict))

res = [test_dict [i] for i in temp]

 
# print result

print ( "The selected values ​​from list keys is:" +   str (res))

Exit:

The original dictionary is: {’Akshat’: 2, ’Manjeet’: 4, ’Nikhil’: 3, ’ Akash ’: 1}
The selective list is: [’ Manjeet ’,’ Nikhil ’]
The selected values ​​from list keys is: [4, 3]

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method