0

I'm able to achieve this but think there must be a better way of doing it. The idea is to have common solution for both the cases.

Problem:

a & b are two dictionaries . values of these dictionaries could be a simple string (use case 1) or lists of same length (use case 2). Please note that length of these lists could be different between a & b (ex:- Key '2' in use case 2) but length within the same dictionary is same.

Expected:

We need to create a final dictionary which will have keys from both the dictionaries a & b and consolidate those values (be it a string or list) as lists of lengths equivalent to longest from either of the dictionaries. For ex:- In use case 2, Key 2 has ["abc","def"] in dictionary a but just ["abc"] in dictionary b.Here we are expecting final result as 2: ["abc","def","abc",None].

Examples:

use case 1:

a = {1:"xyz",2:999,3:1234,4:"boy"}
b = {1:"xyz",2:99,3:19,6:"toy"}

Result expected : {1: ['xyz', 'xyz'], 2: [999, 99], 3: [1234, 19], 4: ['boy', None],
                   6: ['None', 'toy']

use case 2:

a = {1:["xyz","123"],2:["abc","def"],3:["zzz",""],4:["boy","abc"]}
b = {1:["xyz","1234"],2:["abc"],3:["zz",""], 6: ["toy"]}

result expected: {1: [('xyz', 'xyz'), ('123', '1234')], 2: [('abc', 'abc'), ('def', None)], 
                  3: [('zzz', 'zz'), ('', '')], 4: [('boy', None), ('abc', None)], 
                  6: [(None, 'toy')]})
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
MSH
  • 165
  • 2
  • 2
  • 11
  • 1
    Have you seen this: http://stackoverflow.com/questions/1495510/combining-dictionaries-of-lists-in-python – Shubham Namdeo Jan 03 '17 at 07:03
  • 2
    It would be better if you described how the dictionaries need to be joined, deducting that from you minimal examples might be possible but because of the repeated list elements in both dicts, never 100% clear. Please update your post with a **description** of the problem that you try to solve. ( I first thought your output was sorted, until I started wondering where the `None` came from (used to even out unequal length lists?). – Anthon Jan 03 '17 at 07:04
  • Your examples don't make your intent clear, especially how `'toy'` disappears in the output. You need to give us an actual, precise specification of what you want to do. – user2357112 Jan 03 '17 at 07:08
  • Edited question with more details. thanks for your response. – MSH Jan 03 '17 at 20:01

1 Answers1

1

Algorithm:

from itertools import izip_longest

def merge(a, b):
    res = {}
    keys = set(a.keys()) | set(b.keys())
    if isinstance(a.values()[0], list):
        res = {k: list(izip_longest(a.get(k, [None]), b.get(k, [None]))) for k in keys}
    else:
        res = {k: [a.get(k, None), b.get(k, None)] for k in keys}
    return res

Use case 1

a = {1:"xyz",2:999,3:1234,4:"boy"}
b = {1:"xyz",2:99, 3:19,  6:"toy"}

exp = {1: ['xyz', 'xyz'], 
       2: [999, 99], 
       3: [1234, 19], 
       4: ['boy', None],
       6: [None, 'toy']}

print merge(a, b) == exp

Output:

True

Use case 2

a = {1:["xyz","123"], 2:["abc","def"],3:["zzz",""],4:["boy","abc"]}
b = {1:["xyz","1234"],2:["abc"],     3:["zz",""],  6: ["toy"]}
exp = {1: [('xyz', 'xyz'), ('123', '1234')], 
       2: [('abc', 'abc'), ('def', None)],
       3: [('zzz', 'zz'), ('', '')], 
       4: [('boy', None), ('abc', None)],
       6: [(None, 'toy')]}

print merge(a, b) == exp

Output:

True

Note Python 2

Mike Müller
  • 82,630
  • 20
  • 166
  • 161