Syntax for extracting multiple items from a dictionary

S

shark

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"}

Thanks,

shark
 
R

Roy Smith

"shark" <[email protected]> said:
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"}

Thanks,

shark

Just out of curiosity, why would you want to do that? Are you trying to
save on memory to store a lot of these things? If so, I suspect you'd
do even better (a few bytes per object) to store tuples of just the
values, i.e. ("Hoboken", "Alaska"), and unpack them as you need them.

But, to answer your question, I don't know of any standard way to do
what you want. It's easy enough to write (a production version would
probably want to catch KeyError's inside the for loop):

def getDictionarySlice (row, cols):
slice = {}
for key in cols:
slice[key] = row[key]
return slice

This won't work, but it would be kind of cool if it did:

def getOmnicientDictionarySlice (row, cols):
for key not in cols:
del row[key]

Hmmm. Maybe there's an April Fools PEP in there somewhere :)
Actually, you could do:

def deleteUnwantedKeysInPlace (row, cols):
for key in row.keys():
if key not in cols:
del row[key]
 
L

Leif K-Brooks

shark said:
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?
 
S

Stefan Behnel

shark said:
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"}

Untested:

dict( (key,value) for (key,value) in row.iteritems() if key in cols )

Works in Py2.4

Stefan
 
A

anton muhin

Stefan said:
Untested:

dict( (key,value) for (key,value) in row.iteritems() if key in cols )

Works in Py2.4

Stefan

Or dict((key, row[key]) for key in cols).

regards,
anton.
 
B

Bengt Richter

Untested:

dict( (key,value) for (key,value) in row.iteritems() if key in cols )

If there's an overall why for doing it at all, why not just iterate through
keys of interest? I.e., (untested)

dict( (key, row[key]) for key in cols )
Works in Py2.4

Stefan

Regards,
Bengt Richter
 
B

Bengt Richter

]
If there's an overall why for doing it at all, why not just iterate through
keys of interest? I.e., (untested)

dict( (key, row[key]) for key in cols )
Sorry Anton, I didn't see your post. Newsfeed delays seem
to make this kind of duplication fairly likely ;-/

Regards,
Bengt Richter
 
D

Dave Merrill

anton muhin said:
Stefan said:
Untested:

dict( (key,value) for (key,value) in row.iteritems() if key in cols )

Works in Py2.4

Stefan

Or dict((key, row[key]) for key in cols).

regards,
anton.

I'm on Py 2.3.3, and neither of these appear to work. Can someone confirm? I
can't see anything in the 2.4 release notes that point to where this would
have changed.

Thanks,

shark
 
J

Jean Brouwers

The correct syntax is:

dict([(key, row[key]) for key in cols])

i.e. the list must be enclosed in [...].

/Jean Brouwers



Dave Merrill said:
anton muhin said:
Stefan said:
shark schrieb:

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"}


Untested:

dict( (key,value) for (key,value) in row.iteritems() if key in cols )

Works in Py2.4

Stefan

Or dict((key, row[key]) for key in cols).

regards,
anton.

I'm on Py 2.3.3, and neither of these appear to work. Can someone confirm? I
can't see anything in the 2.4 release notes that point to where this would
have changed.

Thanks,

shark
 
P

Peter Hansen

Dave said:
anton muhin said:
Or dict((key, row[key]) for key in cols).

I'm on Py 2.3.3, and neither of these appear to work.

You're probably getting the error shown. Try the change in
the line following it instead.

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]
>>> row = {'fname': 'Frank', 'lname': 'Jones', 'city': 'Hoboken', 'state': 'Alaska'}
>>> cols = ['city', 'state']
>>> dict((key, row[key]) for key in cols)
File "<stdin>", line 1
dict((key, row[key]) for key in cols)
^
SyntaxError: invalid syntax
>>> dict([(key, row[key]) for key in cols])
{'city': 'Hoboken', 'state': 'Alaska'}

I can't see anything in the 2.4 release notes that point to where this would
have changed.

See http://www.python.org/2.4/highlights.html and search for
"generator expressions".

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top