18
x = ['1', '2', '3', '4']
y = [[1,0],[2,0],[3,0],[4,]]

I want to create a dictionary so the x and y values would correspond like this:

1: [1,0], 2: [2,0]

and etc

vijay
  • 2,646
  • 2
  • 23
  • 37
Kara
  • 765
  • 5
  • 11
  • 29

4 Answers4

36

You can use zip function: dict(zip(x, y))

>>> x = ['1', '2', '3', '4']
... y = [[1,0],[2,0],[3,0],[4,]]
>>> dict(zip(x, y))
0: {'1': [1, 0], '2': [2, 0], '3': [3, 0], '4': [4]}
Igonato
  • 10,175
  • 3
  • 35
  • 64
7

In python > 2.7 you can use dict comprehension:

>>> x = ['1', '2', '3', '4']
>>> y = [[1,0],[2,0],[3,0],[4,]]
>>> mydict = {key:value for key, value in zip(x,y)}
>>> mydict
{'1': [1, 0], '3': [3, 0], '2': [2, 0], '4': [4]}
>>> 

Still the best answer has already been given

dict(zip(x, y))

In python <= 2.7 you can use itertools.izip in case you work with big lists as izip returns an iterator. For small lists like yours, the use of izip would be overkilling. Note however that itertools.izip dissapeared in python 3. In python 3, the zip builtin already returns an iterator and in consequence izip was not needed anymore.

joaquin
  • 82,968
  • 29
  • 138
  • 152
3

The quick and easy answer is dict(zip(x,y)), if you're ok with the keys being strings. otherwise, use dict(zip(map(int,x),y))

Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
2

You can use itertools.izip to accomplish this.

from itertools import izip
x = ['1', '2', '3', '4']
y = [[1,0],[2,0],[3,0],[4,]]
dict(izip(x, y))

If your flavor of Python is 3.x, then you can use itertools.zip_longest to do the same thing.

from itertools import zip_longest
x = ['1', '2', '3', '4']
y = [[1,0],[2,0],[3,0],[4,]]
dict(zip_longest(x, y))    
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • So what's wrong with izip? – Makoto Mar 03 '13 at 06:44
  • I've already flagged this question as an exact duplicate, but I don't know why people have down-voted this answer. It's nearly identical to this highly *up*-voted [similar answer](http://stackoverflow.com/a/209880/1988505). – Wesley Baugh Mar 03 '13 at 06:47
  • 2
    probably that 'Simple is better than complex' – joaquin Mar 03 '13 at 06:47
  • @WesleyBaugh good point. but note the answer was from 2008, five years ago. Dict comprehensions were included in py2.7, in 2010, and the dict(zip()) answer, that imho is the best, got x5 more votes and is the one selected – joaquin Mar 03 '13 at 06:54
  • 1
    @joaquin Using a dict comprehension doesn't change the fact that `izip` in py2.7 is still more memory efficient when dealing with large dictionaries. The down-vote tooltip says "This answer is not useful". The fact is, this answer is useful. – Wesley Baugh Mar 03 '13 at 08:01
  • @WesleyBaugh ok I reverse my downvote. It is true that izip can be much faster than zip. Note that this is not the case anymore in py3k where itertools.izip disappeared and good old zip returns an iterator. Still in py27, as the OP was not asking about big lists, IMHO this answer should not be recommended. – joaquin Mar 03 '13 at 09:03
  • @joaquin: In Py3K there is `itertools.zip_longest` which does the same thing as `izip` (with an optional fill value). – Makoto Mar 03 '13 at 17:39