0

I have two function which returns two dictionaries. In third function I've tried merge these dictionaries using update but I've got Noneresult while printing. Could anyone explain my mistake?

def first():
    dict1 = {
        one : [1, 2, 3]
    }
    return dict1

def second():
    dict2 = {
        two : [3, 4, 5]
    }
    return dict2

def third():
    return first().update(second)


print(third())
>>> None

I also tried this way:

def third():
    a = first().copy()
    return a.update(second())

By the way, this way doesn't work in Python 3.4:

def third():
    return dict(a.items() + b.items())
Bazaka Bazaka
  • 135
  • 11
  • 6
    `.update` is **in-place**, it doesn't return a new dictionary. You could add e.g. `list(a.items())` in Python 3, but it seems awkward. – jonrsharpe Mar 30 '16 at 11:26
  • Also you need to *call* `second`, e.g. `d = first(); d.update(second())`. Currently you're just referring to the function object, not the dict it returns. – Alex Riley Mar 30 '16 at 11:27
  • 4
    I think what you want is already answered here: http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression – Miguel Jiménez Mar 30 '16 at 11:30

1 Answers1

1

A bit modified version:

def third():
    d =  first().copy()
    d.update(second())
    return d
gorros
  • 1,411
  • 1
  • 18
  • 29