Is there an easy way to "append()" two dictionaries together in Python?
Posted
by digitaldreamer
on Stack Overflow
See other posts from Stack Overflow
or by digitaldreamer
Published on 2010-04-06T15:57:59Z
Indexed on
2010/04/06
16:03 UTC
Read the original article
Hit count: 115
python
|dictionary
If I have two dictionaries I'd like to combine in Python, i.e.
a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
If I run update on them it reorders the list:
a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
when what I really want is attach "b" to the end of "a":
{'1': 1, '2': 2, '3': 3, '4': 4}
Is there an easy way to attach "b" to the end of "a" without having to manually combine them like so:
for key in b:
a[key]=b[key]
Something like += or append() would be ideal, but of course neither works on dictionaries.
© Stack Overflow or respective owner