python


What is the best way to merge multiple dictionaries?


I currently have a data structure that looks something like:
agents = {
'medic': {'medic1': {...}},
'police': {'police1': {...}, 'police2': {...}},
}
Each key in the sub dictionaries is guaranteed to be unique. I want to create a new dictionary from these dictionaries (I'm marshalling data, before sending it to another program).
My current way is:
new_dict = {}
for d in agents.values():
new_dict.update(d)
assert new_dict == {'medic1': {...}, 'police1': {...}, 'police2': {...}}
This is a bit verbose for something quite simple. There's also this one liner. But as with most one liners it begins to lack clarity.
from itertools import chain
new_dict = dict(chain.from_iterable(d.items() for d in agents.values()))
I also know that in python 3.5 I will be able to do something like:
new_dict = {**d for d in agents.values()}
Is there a smarter way to create this dictionary?
{k: v for d in agents.values() for k, v in d.items()}
Using dict constructor:
A = {"Y":2,"X":4}
B = {"Z":1,"Q":5}
C = dict(A, **B);

Related Links

Python-Instagram user_recent_media returning next of none
Django - AttributeError: 'module' object has no attribute 'admin'
Convert pandas (python) categorical x-axis to continuous values when plotting
How to make a specific type of python face tracker
OSError when trying to delete keys from redis
remove/reset matplotlib quiver arrows
Repeating elements in a list
Run python-rq worker process on application start
Chrome Not Starting When Using ChromeDriver
How to selectively iterate over files with Python
I'm having trouble using docker-py in a development environment on OSX
How to query AWS to get ELB names and attached instances to that using python boto modules?
Pi Wireless AP is dropping shortly after boot
How to redirect python logging output to file instead of stdout?
Matplotlib- Any way to use integers AND decimals in colorbar ticks?
ImportError: No module named setuptools.command on Mac OS X within virtualenv

Categories

HOME
paypal
app-inventor
warnings
wildcard
visual-studio-2013
youtube-livestreaming-api
propertygrid
value
dropbear
dryioc
game-physics
cs-cart
local
phoenix
squarespace
rapidjson
pyyaml
off-canvas-menu
jpeg2000
zend-framework-mvc
propel2
ejbca
sensu
onesignal
lync-2013
rhandsontable
multichoiceitems
bitcoin-testnet
crt
protobuf-net
percentage
pyscripter
polymorphism
vsts-package-management
zero
readfile
image-compression
parent
tic-tac-toe
ddms
ssh.net
chown
pinvoke
livescribe
info
redux-router
sqlexception
iron.io
build-process
spark-cassandra-connector
instruments
nofollow
gmsmapview
usb-drive
eml
test-class
self-hosting
function-fitting
kendo-menu
google-hadoop
sysfs
lexicographic
sysctl
candidate-key
libssh2
facebook-wall
system.web
surrogate-key
smartystreets
level
phpdocx
help-viewer
uipickerviewcontroller
nsmatrix
astyanax
ng-pattern
template-haskell
android-contextmenu
snapjs
transitive-closure-table
datarepeater
rose-db-object
flash-cs5.5
text-size
datacontract
cookieless
noir
open-graph-beta
osx-leopard
aio
google-instant
callgrind
visual-studio-2010-beta-2
winsnmp
data-entry
web-analytics-tools
interface-design
self-reference

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App