-1

I have a three lists and I would like to build a dictionary using them:

a = [a,b,c]  
b = [1,2,3]  
c = [4,5,6]  

i expect to have:

{'a':1,4, 'b':2,5, 'c':3,6}

All I can do now is:

{'a':1,'b':2, 'c':3}

What should i do?

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
lukas
  • 31
  • 1
  • 1
  • 2
    The output you expect isn't a valid literal. Do you want the values to be lists? Try searching for that. – jonrsharpe Aug 26 '17 at 17:34
  • You should correct your creation of list a, did you mean some string literals instead of ,a,b,c?, do you want tuple () or list [] as a value for each key? – nio Aug 26 '17 at 17:37
  • Your expected result does not look like a python dictionary nor any python object. – Stop harming Monica Aug 26 '17 at 17:43
  • What have you tried yourself? Please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and [How much research effort is expected of Stack Overflow users](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – FluffyKitten Aug 26 '17 at 17:46
  • 1
    Possible duplicate of [Convert a list to a dictionary in Python](https://stackoverflow.com/questions/4576115/convert-a-list-to-a-dictionary-in-python) – parik Aug 27 '17 at 00:22

3 Answers3

5

You can try this:

a = ["a","b","c"] 
b = [1,2,3]  
c = [4,5,6]  

new_dict = {i:[j, k] for i, j, k in zip(a, b, c)}

Output:

{'b': [2, 5], 'c': [3, 6], 'a': [1, 4]}

If you really want a sorted result, you can try this:

from collections import OrderedDict

d = OrderedDict()

for i, j, k in zip(a, b, c):
    d[i] = [j, k]

Now, you have an OrderedDict object with the keys sorted alphabetically.

Ajax1234
  • 66,333
  • 7
  • 57
  • 95
3

check this: how to zip two lists into new one i suggest to first zip the b and c lists and then map them into a dictionary again using zip:

a = ['a','b','c']
b = [1,2,3]
c = [4,5,6]
vals = zip(b,c)
d = dict(zip(a,vals))
print(d)
nio
  • 5,031
  • 2
  • 22
  • 35
  • Thank you nio. This is exactly what i needed. In fact i have tried already to zip 2 list but i got . and I could not print them. Funny is it i working all together. – lukas Aug 26 '17 at 18:15
1
a = ['a','b','c']  
b = [1,2,3]  
c = [4,5,6] 

result = { k:v  for k,*v in zip(a,b,c)}

RESULT

print(result)
Fuji Komalan
  • 1,851
  • 12
  • 24
  • 1
    Can you explain your code and why it solves the issue? *Code only* answers are often not that helpful. – Zabuzard Aug 30 '17 at 09:41