I want to transform
l = ['a','b','c','d']
to
d = {'a': 0, 'b': 1, 'c': 2, 'd': 3}
The best solution I have so far is that one:
d = {l[i]:i for i in range(len(l))}
Is there more elegant way to do this?
d = {e:i for i, e in enumerate(l)}
Edit: As @LeonYoung suggested, if you want to be compatible with python < 2.7 (despite the tag), you must use
d = dict((e, i) for i, e in enumerate(l))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments