Syntax for extracting multiple items from a dictionary

Leif K-Brooks eurleif at ecritters.biz
Tue Nov 30 15:13:07 CET 2004


shark wrote:
> row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" :
> "Alaska"}
> cols = ("city", "state")
> 
> Is there a best-practices way to ask for an object containing only the keys
> named in cols out of row? In other words, to get this:
> {"city" : "Hoboken", "state" : "Alaska"}

Why would you need to do that? There's nothing you can do to the second 
dictionary that you can't do to the first, so what's wrong with leaving 
the extra items in place?



More information about the Python-list mailing list