121

I have two lists of the same length:

[1,2,3,4] and [a,b,c,d]

I want to create a dictionary where I have {1:a, 2:b, 3:c, 4:d}

What's the best way to do this?

agf
  • 171,228
  • 44
  • 289
  • 238
SuperString
  • 21,593
  • 37
  • 91
  • 122
  • the downside of stackoverflow is that there is even less incentive to rtfm: http://docs.python.org/library/stdtypes.html#dict –  Sep 01 '11 at 15:33

6 Answers6

205
dict(zip([1,2,3,4], [a,b,c,d]))

If the lists are big you should use itertools.izip.

If you have more keys than values, and you want to fill in values for the extra keys, you can use itertools.izip_longest.

Here, a, b, c, and d are variables -- it will work fine (so long as they are defined), but you probably meant ['a','b','c','d'] if you want them as strings.

zip takes the first item from each iterable and makes a tuple, then the second item from each, etc. etc.

dict can take an iterable of iterables, where each inner iterable has two items -- it then uses the first as the key and the second as the value for each item.

agf
  • 171,228
  • 44
  • 289
  • 238
  • 3
    thanks for pointing me to `izip_longest`, I needed exactly this. – Johannes P Sep 10 '13 at 11:35
  • 3
    izip() is removed in Python3 , simply use zip() – im_bhatman Jan 28 '19 at 07:44
  • 1
    This is how an answer on stack should be! A clear example on top you can easily copy, a brief description to point to the right direction, links to learn more and it's short, you have the whole answer in one page. – nnsense Jun 02 '20 at 19:35
21
>>> dict(zip([1, 2, 3, 4], ['a', 'b', 'c', 'd']))
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

If they are not the same size, zip will truncate the longer one.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
7
dict(zip([1,2,3,4], ['a', 'b', 'c', 'd']))

http://docs.python.org/library/functions.html

Elf Sternberg
  • 16,129
  • 6
  • 60
  • 68
6

If there are duplicate keys in the first list that map to different values in the second list, like a 1-to-many relationship, but you need the values to be combined or added or something instead of updating, you can do this:

i = iter(["a", "a", "b", "c", "b"])
j = iter([1,2,3,4,5])
k = list(zip(i, j))
for (x,y) in k:
    if x in d:
        d[x] = d[x] + y #or whatever your function needs to be to combine them
    else:
        d[x] = y

In that example, d == {'a': 3, 'c': 4, 'b': 8}

Dan Mantyla
  • 1,840
  • 1
  • 22
  • 33
5

I don't know about best (simplest? fastest? most readable?), but one way would be:

dict(zip([1, 2, 3, 4], [a, b, c, d]))
Remy Blank
  • 4,236
  • 2
  • 23
  • 24
0

I found myself needing to create a dictionary of three lists (latitude, longitude, and a value), with the following doing the trick:

> lat = [45.3,56.2,23.4,60.4]
> lon = [134.6,128.7,111.9,75.8]
> val = [3,6,2,5]
> dict(zip(zip(lat,lon),val))
{(56.2, 128.7): 6, (60.4, 75.8): 5, (23.4, 111.9): 2, (45.3, 134.6): 3}

or similar to the above examples:

> list1 = [1,2,3,4]
> list2 = [1,2,3,4]
> list3 = ['a','b','c','d']
> dict(zip(zip(list1,list2),list3))
{(3, 3): 'c', (4, 4): 'd', (1, 1): 'a', (2, 2): 'b'}

Note: Dictionaries are "orderless", but if you would like to view it as "sorted", refer to THIS question if you'd like to sort by key, or THIS question if you'd like to sort by value.

Community
  • 1
  • 1
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110