0

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
  • 115,751
  • 26
  • 228
  • 437
lukas
  • 49
  • 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

4 Answers4

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
  • 69,937
  • 8
  • 61
  • 102
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,141
  • 2
  • 24
  • 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,979
  • 16
  • 25
  • 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
0
Promotion_or_Not = "Yes","Yes","No","Yes","Yes","Yes","Yes","No","Yes","No"
Nopay_leave_or_not ="No","No","Yes","Yes","No","Yes","Yes","No","Yes","No"

Last_year_evaluation = "Good","Bad","Moderate","Good","Moderate","Moderate","Good","Bad","Good","Moderate"


mydict = {'Promotion_or_Not': [Promotion_or_Not],'Nopay_leave_or_not': [Nopay_leave_or_not], 'Last_year_evaluation':[Last_year_evaluation] }
print(mydict)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    This ans not adressing the qus at all. You just statically assign variable instead of following the example, provided in the qus. – Mazhar Mar 31 '23 at 13:33
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34119868) – Mazhar Mar 31 '23 at 13:34