2

I know this question has been asked but I could not find anything that appends the values to the list instead of creating a list of list. I have two dictionaries which have identical values:

dictionary1 = {'1':'one', '2':'two', '3':'three'}
dictionary2 = {'1':['uno'], '2':['dos'], '3':['tres']}

and I need it to return this:

combined = {'1':['one','uno'] '2':['two','dos'] '3':['three',tres']} 

so far everything I tried returns this:

combined = {'1':['one'['uno']] '2':['two'['dos']] '3':['three'[tres']]}

which has nested lists. How do I append the values of dictionary 1 into the dictionary2 list? Please help me, I know is really simple but I don't know how to do it. thank you

Here is my code:

    combined = {key:[dictionary1[key], dictionary2[key]] for key in dictionary1}
April
  • 213
  • 1
  • 3
  • 15
  • Erm.. that code should work, and give you what you want. – DSM Sep 23 '15 at 22:05
  • Your code returns what you want. – areuexperienced Sep 23 '15 at 22:06
  • @DSM that is giving me combined = {'1':['one',['uno']] '2':['two',['dos']] '3':['three',[tres']]} I don't understand why. is there another way to do it? – April Sep 23 '15 at 22:06
  • @April: respectfully, I don't believe you. :-) If you copy and paste your first two lines into a console, and then your `combined = {key: et cetera` line, you'll get your target. Maybe your `dictionary2` is wrong, and has lists instead of strings as values. – DSM Sep 23 '15 at 22:07
  • Post your full code, there is something you are not showing us. – rlbond Sep 23 '15 at 22:07
  • YES! that's what it is. one of my dictionaries has a list for the values. I just realized. So how do I append the other value to the list? – April Sep 23 '15 at 22:09
  • possible duplicate of [Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?](http://stackoverflow.com/questions/11011756/is-there-any-pythonic-way-to-combine-two-dicts-adding-values-for-keys-that-appe) – DevLounge Sep 23 '15 at 22:19

3 Answers3

2

Actually, your code is already correct?

>>> dictionary1 = {'1':'one', '2':'two', '3':'three'}
>>> dictionary2 = {'1':'uno', '2':'dos', '3':'tres'}
>>> combined = {key:[dictionary1[key], dictionary2[key]] for key in dictionary1}
>>> combined
{'3': ['three', 'tres'], '2': ['two', 'dos'], '1': ['one', 'uno']}

Are you sure you are not doing anything else besides this?

proycon
  • 495
  • 3
  • 9
  • I just realized one of my dictionaries has values as list, so what I need is to append the new values to that list. how do I do that? – April Sep 23 '15 at 22:11
  • If dictionary2 for instance has lists as values, then you can just do this: ``combined = {key:[dictionary1[key]] + dictionary2[key] for key in dictionary1}`` .. if dictionary1 holds lists as values as well then remove the surrounding ``[]`` for ``dictionary1[key]``. We're just concatenating lists here. – proycon Sep 23 '15 at 22:13
  • how could you do this in separate lines with a for loop? – April Sep 23 '15 at 23:14
1
dictionary1 = {'1':'one', '2':'two', '3':'three'}
dictionary2 = {'1':['uno'], '2':['dos'], '3':['tres']}

combined = {key:[dictionary1[key], dictionary2[key][0]] for key in dictionary1}

Simply retrieve the 0th index from the values of dictionary2, considering they're all lists of length 1.

Otherwise, this will work:

combined = {key:[dictionary1[key]] + dictionary2[key] for key in dictionary1}

This basically creates a one element list out of the values of dictionary1 and combines it with the values of dictionary2 which are already lists.

0
dictionary1 = {'1':'one', '2':'two', '3':'three'}
dictionary2 = {'1':'uno', '2':'dos', '3':'tres'}


z = zip(zip(dictionary1.keys(), dictionary2.keys()),
        zip(dictionary1.values(), dictionary2.values()))

dual = {}

for el in z:
    dual[el[0][0]] = el[1]


print(list(dual.items()))

OUPUT

[('2', ('two', 'dos')), ('1', ('one', 'uno')), ('3', ('three', 'tres'))]

LetzerWille
  • 5,355
  • 4
  • 23
  • 26