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

Checking that array doesn't contain negative numbers, and running function again if it does
Matplotlib and non-ascii characters
How does HttpResponse(status=<code>) work in django?
Write a CSV from Urlib and manage encoding properly
python - list index out of range, working with CSV?
Pandas DataFrame get substrings from column
Python GET is not working in browser
Django, Pinax, couldn't extract file
Getting proper list of members from pymongo
Not writing into an Excel file
How to integrate my python plugin with nagios
Find words containing . in middle or at the end
Using apply on a column
Maximum-sum subarray given constraints on indices
Converting text to columns
easily make labels look nice pandas plotting

Categories

HOME
jsf
security
signalr
tinyos
visual-studio-2013
wms
tomcat7
wampserver
orientation
user
triggers
nodemailer
uiview
webmethods
cratedb
symfony2-easyadmin
netezza
ada
flat-file
ms-access-2007
esql
go-cd
memsql
samsung-mobile
python-textprocessing
upnp
spring-data-neo4j
fetch-api
frp
reactiveui
cell-array
referenceerror
picturebox
polymorphism
ifstream
web-audio-api
body-parser
phpspreadsheet
pace
xen
nlb
vmd
office365connectors
cross-entropy
flex4.5
cgo
exiftool
knockout-3.0
service-fabric-stateful
median
estimation
spring-data-hadoop
selenium-firefoxdriver
unity3d-editor
emoticons
ingres
onresume
android-navigationview
pdflib
windows-azure-pack
easing
bacnet
linkageerror
image-editing
group-concat
handlebars.java
android-recyclerview
loose-typing
java.util.calendar
geodjango
apigee-baas
vine
ytplayerview
gmaps4rails
node-inspector
google-admin-audit-api
snmp4j
jstat
facebook-wall
android-looper
ifft
dvcs
bounds
initialization-vector
roxygen
sqljdbc
modeshape
facebook-sdk-3.1
installshield-2009
multiple-conditions
iconv
client-library
android-contextmenu
dropdownlistfor
flexicious
qtembedded
gethashcode
clause
delegatecommand
front-controller

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