-3

I have a dictionary as such:

dict = {1:a, 2:b, 3:c, 4:d}

I have a list of dict keys in a list already:

keys_order = [4, 2, 3, 1] 

Given the info above, I want to create a list with 'values' of the dict according to the order of keys_order.

Expected result:

values_order = [d, b, c, a]

How should I do it?

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
chen abien
  • 257
  • 1
  • 6

1 Answers1

0
dict = {1:'a', 2:'b', 3:'c', 4:'d'}
keys_order = [4, 2, 3, 1]
values_order = []

for key in keys_order:
    values_order.append(dict[key])

print(values_order)

Output:

['d', 'b', 'c', 'a']