3

Now, I wanna convert an array to a dict like this:

dict = {'item0': arr[0], 'item1': arr[1], 'item2': arr[2]...}

How to solve this problem elegantly in python?

1
  • 1
    It's a little weird to me that you would want to do this. Why change a nicely structured list/array into a dictionary? You haven't added any information -- If anything, you've made it more difficult to recover the original order. . . Commented Sep 30, 2014 at 5:16

7 Answers 7

13

You could use enumerate and a dictionary comprehension:

>>> arr = ["aa", "bb", "cc"]
>>> {'item{}'.format(i): x for i,x in enumerate(arr)}
{'item2': 'cc', 'item0': 'aa', 'item1': 'bb'}
Sign up to request clarification or add additional context in comments.

2 Comments

why I got invalid syntax Error when I tested your code? There is an arrow pointing to 'for'. I did not change anything.
@city -- What version of python are you using? If you're on python2.6, then dict-comprehensions haven't been created yet. You'd need to fall back on the old dict constructor: dict(('item{0}'.format(i), x) for i, x in enumerate(arr))
2

Suppose we have a list of ints:

We can use a dict comprehension

>>> l = [3, 2, 4, 5, 7, 9, 0, 9]
>>> d = {"item" + str(k): l[k] for k in range(len(l))}
>>> d
{'item5': 9, 'item4': 7, 'item7': 9, 'item6': 0, 'item1': 2, 'item0': 3, 'item3': 5, 'item2': 4}

Comments

2

You can also use the dict() to construct your dictionary.

d = dict(('item{}'.format(i), arr[i]) for i in xrange(len(arr)))

1 Comment

Why down vote here? If you down vote an answer please mention the reason so that it can be improved.
1
simpleArray = [ 2, 54, 32 ]
simpleDict = dict()
for index,item in enumerate(simpleArray):
    simpleDict["item{0}".format(index)] = item

print(simpleDict)

Ok, first line Is the input, second line is an empty dictionary. We will fill it on the fly.

Now we need to iterate, but normal iteration as in C is considered non Pythonic. Enumerate will give the index and the item we need from the array. See this: Accessing the index in Python 'for' loops.

So in each iteration we will be getting an item from array and inserting in the dictionary with a key from the string in brackets. I'm using format since use of % is discouraged. See here: Python string formatting: % vs. .format.

At last we will print. Used print as function for more compatibility.

Comments

1

you could use a dictionary comprehension eg.

>>> x = [1,2,3]
>>> {'item'+str(i):v for i, v in enumerate(x)}
>>> {'item2': 3, 'item0': 1, 'item1': 2}

Comments

1

Use dictionary comprehension: Python Dictionary Comprehension

So it'll look something like:

d = {"item%s" % index: value for (index, value) in enumerate(arr)}

Note the use of enumerate to give the index of each value in the list.

Comments

0

Using map, this could be solved as:

a = [1, 2, 3]
d = list(map(lambda x: {f"item{x[0]}":x[1]}, enumerate(a)))

The result is:

[{'item0': 1}, {'item1': 2}, {'item2': 3}]

1 Comment

He asked for a dict, not list, as an outer type. It should not start with "[{...", it should start with "{..."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.