Combining two dictionaries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Combining two dictionaries

    HI,

    I have two or more dictionaries.I have to combine the ldictionaries and make one list.I am trying to do the following and I am getting the following error.How to fix this bug?

    Code:
    dict1 = {100:[1,2],200:[2,3]}
    dict2 = {300:[1,2],400:[2,3]}
    dict3 = dict1 + dict2
    Traceback (most recent call last):
    File "<pyshell#6 >", line 1, in -toplevel-
    dict3 = dict1 + dict2
    TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

    Thanks
    PSB
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by psbasha
    HI,

    I have two or more dictionaries.I have to combine the ldictionaries and make one list.I am trying to do the following and I am getting the following error.How to fix this bug?

    Code:
    dict1 = {100:[1,2],200:[2,3]}
    dict2 = {300:[1,2],400:[2,3]}
    dict3 = dict1 + dict2
    Traceback (most recent call last):
    File "<pyshell#6 >", line 1, in -toplevel-
    dict3 = dict1 + dict2
    TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

    Thanks
    PSB
    hello, you should read the reference library here on dict, or use help(dict) before asking here. the update() method is what you need.
    Code:
    >>> a = {'1':'one'}
    >>> b = {'2':'two'}
    >>> a.update(b)
    >>> a
    {'1': 'one', '2': 'two'}

    Comment

    Working...