Find value values in a list consisting of keys in a dictionary

Asked 2 years ago, Updated 2 years ago, 13 views

We created dict={a:1,b:2,c:4,d:8} and list=[a,b] I want to make a list with the values of a and b as factors, ->[1,2] Like this What should I do?ㅜㅜ/

python

2022-09-22 08:24

1 Answers

dict={"a":1, "b":2, "c":4, "d":8}
list= ["a","b"]

selected_values = [dict[a] for a in list]
print(selected_values)

Use the feature list compression.


2022-09-22 08:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.