0

I have a list with size about 30000: ['aa', 'bb', 'cc', 'dd', ...], from this list, I want to build a dict which maps element to index, so the result dict is {'aa': 0, 'bb': 1, 'cc': 2, 'dd': 3, ...}. Here comes my code:

cnt = 0
mp = {}
for name in name_list:
    mp[name] = cnt
    cnt += 1
return mp

It seems my code is not concise and effective, so how to improve it?

vinllen
  • 1,369
  • 2
  • 18
  • 36

3 Answers3

5

The shortest is to use enumerate and a dict comprehension, I guess:

mp = {element: index for index, element in enumerate(name_list)}
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • In my test, this code returns wrong map:{'aa': 0, 'cc': 2, 'dd': 3, 'bb': 1} – vinllen Oct 12 '16 at 12:41
  • cc should map to 3 – vinllen Oct 12 '16 at 12:42
  • @vinllen: in your test data, 'cc' is the third element (so index 2) and 'dd' is the third (so index 3). Others have already commented on your question that your data or result seem to be wrong. – RemcoGerlich Oct 12 '16 at 12:43
  • sorry about make a mistake – vinllen Oct 12 '16 at 12:44
  • 1
    @vinllen you need to look carefully at the output from your own code. If you print the whole dictionary, the [order of the elements is not fixed](http://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary). This code neatly matches your own. – roganjosh Oct 12 '16 at 12:45
3

You can use enumerate to create the mapping between list object and the indices, then call dict on the reversed outputs:

mp = dict(tup[::-1] for tup in enumerate(name_list))
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
1

How about using the list index for each item?

mp = {item: name_list.index(item) for item in name_list}
Baryo
  • 314
  • 1
  • 9
  • That's completely inefficient. – vishes_shell Oct 12 '16 at 13:14
  • 2
    `.index()` has to loop through the entire list until it finds the item. This way it has to do that for each item, so it'll be very very slow with 30k items. Worse, if an item occurs twice in the list, this will give the same index twice, so it'll be wrong as well. – RemcoGerlich Oct 12 '16 at 13:16