0

I need to create a dictionary with these keys 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 and these values 1, 123, 8765, 0987, 182735, 3459, 9, 0, 2 ,835, 874

I already put these values and keys into lists, a list for each.

year = [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]
value = [1, 123, 8765, 0987, 182735, 3459, 9, 0, 2 ,835, 874]

I want a diccionary that has key 2007 value 1 , 2008 123 and so.

I've tried dic[key]=value but it didn't work, i've tried function fromkeys but it didn't work either. please help!!!!

sacuL
  • 49,704
  • 8
  • 81
  • 106
Carolina
  • 45
  • 1
  • 5
  • 4
    Possible duplicate of [Map two lists into a dictionary in Python](https://stackoverflow.com/questions/209840/map-two-lists-into-a-dictionary-in-python) – sjw May 20 '18 at 19:40

3 Answers3

4
>>> dict(zip(year, value))
{2007: 1, 2008: 123, 2009: 8765, 2010: 987, 2011: 182735, 2012: 3459, 2013: 9, 2014: 0, 2015: 2, 2016: 835, 2017: 874}
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0
year = [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]
value = [1, 123, 8765, 987, 182735, 3459, 9, 0, 2 ,835, 874]

s = dict(zip(year, value))
print(s)  
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
-1

dic[key] = value must work provided the key and value are placed in a loop. A simple way to combine keys and values from two lists is using the zip built-in:

dct = dict(zip(year, value))

You can also use a dict comprehension:

dct = {y: v for y, v in zip(year, value)}
user4815162342
  • 141,790
  • 18
  • 296
  • 355