-4

Having dictionary like this

d = {
        'airplane': 0, 
        'automobile': 1, 
        'bird': 2, 
        'cat': 3, 
        'deer': 4, 
        'dog': 5, 
        'frog': 6, 
        'horse': 7, 
        'ship': 8, 
        'truck': 9
}

and a list

l = [3, 4, 1, 7, 9, 0]

how can i create new list conditioning to dictionary

new_list = ['cat', 'deer', 'automobile', 'horse', 'truck', 'airplane']
Saleem Ali
  • 1,363
  • 11
  • 21
ClaWnN
  • 49
  • 7
  • I'm not sure that should be a dictionary to start with; two keys can have the same value. But basically: iterate over l, search for the (first?) key in the dictionary that has that value, put the key in a new list. – jonrsharpe Sep 20 '19 at 08:57
  • what do expect from the dict `d = {'airplane': 0, 'automobile': 0, 'bird': 0}` ? – RomanPerekhrest Sep 20 '19 at 09:01
  • 1
    Possible duplicate of [Python reverse / invert a mapping](https://stackoverflow.com/questions/483666/python-reverse-invert-a-mapping) – norok2 Sep 20 '19 at 09:06
  • new list should represent the key against the value. so, if l has 2, new list must give 'automobile' – ClaWnN Sep 20 '19 at 09:06
  • @ClaWnN, are you aware that dict keys are unordered? therefore, why it must *give 'automobile'* ? – RomanPerekhrest Sep 20 '19 at 09:08
  • @RomanPerekhrest sorry i meant to say 1 – ClaWnN Sep 20 '19 at 09:12
  • @RomanPerekhrest depending on the Python version. For 3.6+, the order of dict keys is [consistent](https://stackoverflow.com/a/39537308/5218354). – norok2 Sep 20 '19 at 09:38

5 Answers5

1

What about this?

d = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
l = [3, 4, 1, 7, 9, 0]
reversed_d = {v: k for k, v in d.items()}
new_list = [reversed_d[i] for i in l]
print(new_list)
# ['cat', 'deer', 'automobile', 'horse', 'truck', 'airplane']

Note that in general, generating reversed_d will not play nicely with duplicates.

norok2
  • 25,683
  • 4
  • 73
  • 99
0

First of all, the dictionary d in your example is not an ideal lookup structure as dictionaries are indexed with their keys and not values. Furthermore, there is no guarantee that a value is unique across all keys.

If you are sure the values are unique, you can change your dictionary around:

d = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
di = {value: key for key, value in d.items()}

After that, getting the second list is just another list comprehension:

l = [3, 4, 1, 7, 9, 0]
new_list = [di[key] for key in l]
andyn
  • 577
  • 1
  • 6
  • 18
0

Simply try like this as below:

d = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
l = [3, 4, 1, 7, 9, 0]

new_list = [list(d)[i] for i in l]
0
from collections import OrderedDict
d1 = OrderedDict(d)
d1_keys, d1_values = list(d1.keys()), list(d1.values())
new_list = [d1_keys[d1_values.index(i)] for i in l]
oaixnah
  • 44
  • 2
0

You just need to fetch the keys from dictionary "d" with the values corresponding to the numbers that are present in the list "l". You can do this easily by using a List Comprehension.

print([list(d.keys())[list(d.values()).index(num)] for num in l])

Since you are working in python-3, d.keys() & d.values() won't return a list; so you need to do that yourself, hence they are type-casted using list()

Output

['cat', 'deer', 'automobile', 'horse', 'truck', 'airplane']
Adarsh Pai
  • 26
  • 2