469,299 Members | 2,093 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Combining two dictionaries

440 256MB
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. dict1 = {100:[1,2],200:[2,3]}
  3. dict2 = {300:[1,2],400:[2,3]}
  4. dict3 = dict1 + dict2
  5.  
  6.  
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
Mar 27 '07 #1
1 8927
ghostdog74
511 Expert 256MB
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. dict1 = {100:[1,2],200:[2,3]}
  3. dict2 = {300:[1,2],400:[2,3]}
  4. dict3 = dict1 + dict2
  5.  
  6.  
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.
Expand|Select|Wrap|Line Numbers
  1. >>> a = {'1':'one'}
  2. >>> b = {'2':'two'}
  3. >>> a.update(b)
  4. >>> a
  5. {'1': 'one', '2': 'two'}
  6.  
Mar 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Kerry Neilson | last post: by
reply views Thread by Till Plewe | last post: by
8 posts views Thread by Frohnhofer, James | last post: by
3 posts views Thread by Shivram U | last post: by
210 posts views Thread by Christoph Zwerschke | last post: by
8 posts views Thread by placid | last post: by
1 post views Thread by Edwin.Madari | last post: by
14 posts views Thread by cnb | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.