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?

André Teixeira
  • 2,042
  • 2
  • 24
  • 39
city
  • 1,724
  • 2
  • 25
  • 38
  • 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. . . – mgilson Sep 30 '14 at 05:16

7 Answers7

11

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'}
DSM
  • 291,791
  • 56
  • 521
  • 443
  • why I got invalid syntax Error when I tested your code? There is an arrow pointing to 'for'. I did not change anything. – city Sep 30 '14 at 04:57
  • @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))` – mgilson Sep 30 '14 at 05:15
  • @mgilson, yeah, you are right. It works now. Thanks. – city Sep 30 '14 at 05:17
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}
icedtrees
  • 5,116
  • 4
  • 23
  • 33
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.

Community
  • 1
  • 1
eri0o
  • 1,487
  • 2
  • 18
  • 35
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}
ragingSloth
  • 1,010
  • 7
  • 21
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.

Community
  • 1
  • 1
Cameron Lee
  • 635
  • 6
  • 11
1

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

d = dict(('item{}'.format(i), arr[i]) for i in xrange(len(arr)))
salmanwahed
  • 8,337
  • 5
  • 30
  • 51
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}]
sudar
  • 111
  • 1
  • 5