Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i pass each item in a list into a dictionary and recieve a list of outputs in return?

My question is difficult to word, what I mean is:

my_dict = {'a': 1, 'b': 2, 'c': 3}
my_list = ['b', 'a', 'b', 'c', 'c']
my_list_output = some_func(my_list, my_dict)
print(my_list_output)
# [2, 1, 2, 3, 3]

Is there a neat, somewhat readable way to do this? Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Chack this

def some_func(my_dict,my_list):
   newList=[my_dict[k] for k in my_list if k in my_dict]
   return newList


my_dict = {'a': 1, 'b': 2, 'c': 3}
my_list = ['b', 'a', 'b', 'c', 'c']
my_list_output= some_func( my_dict,my_list)

print (my_list_output)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading