-1

I keep getting an empty dictionary

#!/usr/local/bin/python3


dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dictNew = {}


def concatDict(dictCon):
    dictNew = dict.update(dictCon)
    return dictNew


concatDict(dic1)
concatDict(dic2)
concatDict(dic3)

print(dictNew)

dictNew is not getting updated from the function calls.

Can someone point me in the right direction?

Keefer
  • 11
  • 1

3 Answers3

1

For joining dictionaries you can simply use this code:

dict1 = {1: 10, 2: 20}
dict2 = {3: 30, 4: 40}
dict3 = {5: 50, 6: 60}
dict_new = {**dic1, **dic2, **dic3}
print(dict_new)

Result:

{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
Lev Zakharov
  • 2,409
  • 1
  • 10
  • 24
0

You want to update dictNew with the dictCon dictionary argument. Since dictionaries are mutable, you don't need to save or return the result as dictNew will be mutated:

#!/usr/local/bin/python3

dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dictNew = {}

def concatDict(dictCon):
    dictNew.update(dictCon)

concatDict(dic1)
concatDict(dic2)
concatDict(dic3)

print(dictNew)

which gives:

{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

However note that your function is literally just masking dictNew.update, so you might as well use that method call instead of this wrapping function:

...
dictNew.update(dic1)
dictNew.update(dic2)
dictNew.update(dic3)
...

A different method is to use the **-operator to explode the dicionaries:

{**dic1, **dic2, **dic3}
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
  • @LevZakharov The function is called `concatDict`, yes `dictNew` could be passed in but if the *purpose of the funciton* is to concat to `dictNew`, then I don't see this is as bad practice. However if this *is* the sole purpose, then why not just use `dictNew.update(...)`! :p – Joe Iddon Sep 03 '18 at 19:14
  • Global variables are __always__ a bad practice. – Lev Zakharov Sep 03 '18 at 19:18
  • @LevZakharov No, they're not: https://stackoverflow.com/a/19158418/7434365. In controlled cases where the operation is simple and known, they are perfectly acceptable. – Joe Iddon Sep 03 '18 at 19:21
  • Here is no reason to use global variables. If you can not use them - do not use them. – Lev Zakharov Sep 03 '18 at 19:24
  • I am trying to practice functions and dictionaries at the same time. This change def concatDict(dictCon): dictNew.update(dictCon) did excatly what I was hoping for. – Keefer Sep 03 '18 at 19:25
  • @LevZakharov Each to themselves, but personally I consider this a simple enough application that demonstrates the concept of mutable objects. – Joe Iddon Sep 03 '18 at 19:26
0

You can make your concatDicts function to accept a variable number of dict as input and return a new merged dict

>>> dic1 = {1:10, 2:20}
>>> dic2 = {3:30, 4:40}
>>> dic3 = {5:50, 6:60}
>>>
>>> def concatDicts(*dicts):
...     return dict((k,v) for dic in dicts for k,v in dic.items())
... 
>>>
>>> new_dic = concatDicts(dic1, dic2, dic3)
>>> print(new_dic)
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
Sunitha
  • 11,777
  • 2
  • 20
  • 23