Transforming list to dict with list items as keys and list indices as values in python

ov7a

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?

fjarri
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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get indices of items in numpy array, where values is in list

From Java

Python dictionary: Get list of values for list of keys

From Dev

Creating Python dictionaries with keys based on items in list of list

From Dev

Python: Making a dict from tuple keys with multiple values from a nested list

From Dev

Python Tuple to Dict, with additional list of keys

From Dev

How to build a dict of indices of occurences of elements in a list in Python?

From Dev

Finding the indices of the values of one list in another list

From Dev

filtering the keys of a python dictionary for all the items of a list

From Dev

Swapping indices with values in a Python list?

From Dev

Python: group list items in a dict

From Dev

create a dict with unique keys and various list values from a tuple

From Dev

List to Dictionary: Even items as keys, odd items as values

From Dev

Transforming/GroupingBy values inside List

From Dev

Python: Update the dict[key] values list only with values not already in list

From Dev

Finding the Indices of multiple items in a list, using a list

From Dev

Ansible: Transforming list to dictionary with items2dict

From Dev

How to fill an array in python with given list of values and list of indices?

From Dev

Python: Making a dict from tuple keys with multiple values from a nested list

From Dev

An efficient way of replacing list of keys with dict values?

From Dev

Transforming a List to a Pivot Table with Python

From Dev

Having list of keys, get list/tuple of values from dict

From Dev

create a dict with unique keys and various list values from a tuple

From Dev

Problems while transforming a python dict to a list of triples?

From Dev

Retrieve dict values from a list in Python

From Dev

Python count and average value of keys in list of dict

From Dev

Python convert list of dictionaries to dict of key, values as list

From Dev

Add missing keys to `dict` in `list`

From Dev

Subset items in list of list based on indices

From Dev

How to print keys and multiples values associated with the keys in the Python list with dict values?

Related Related

  1. 1

    Get indices of items in numpy array, where values is in list

  2. 2

    Python dictionary: Get list of values for list of keys

  3. 3

    Creating Python dictionaries with keys based on items in list of list

  4. 4

    Python: Making a dict from tuple keys with multiple values from a nested list

  5. 5

    Python Tuple to Dict, with additional list of keys

  6. 6

    How to build a dict of indices of occurences of elements in a list in Python?

  7. 7

    Finding the indices of the values of one list in another list

  8. 8

    filtering the keys of a python dictionary for all the items of a list

  9. 9

    Swapping indices with values in a Python list?

  10. 10

    Python: group list items in a dict

  11. 11

    create a dict with unique keys and various list values from a tuple

  12. 12

    List to Dictionary: Even items as keys, odd items as values

  13. 13

    Transforming/GroupingBy values inside List

  14. 14

    Python: Update the dict[key] values list only with values not already in list

  15. 15

    Finding the Indices of multiple items in a list, using a list

  16. 16

    Ansible: Transforming list to dictionary with items2dict

  17. 17

    How to fill an array in python with given list of values and list of indices?

  18. 18

    Python: Making a dict from tuple keys with multiple values from a nested list

  19. 19

    An efficient way of replacing list of keys with dict values?

  20. 20

    Transforming a List to a Pivot Table with Python

  21. 21

    Having list of keys, get list/tuple of values from dict

  22. 22

    create a dict with unique keys and various list values from a tuple

  23. 23

    Problems while transforming a python dict to a list of triples?

  24. 24

    Retrieve dict values from a list in Python

  25. 25

    Python count and average value of keys in list of dict

  26. 26

    Python convert list of dictionaries to dict of key, values as list

  27. 27

    Add missing keys to `dict` in `list`

  28. 28

    Subset items in list of list based on indices

  29. 29

    How to print keys and multiples values associated with the keys in the Python list with dict values?

HotTag

Archive