469,282 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Syntax for extracting multiple items from a dictionary

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
Jul 18 '05 #1
10 10309
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?
Jul 18 '05 #2
In article <Tu********************@rcn.net>, "shark" <no@spam.none>
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"}

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]
Jul 18 '05 #3


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
Jul 18 '05 #4
Stefan Behnel wrote:


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.
Jul 18 '05 #5
On Tue, 30 Nov 2004 15:20:19 +0100, Stefan Behnel <be*******@gkec.informatik.tu-darmstadt.de> wrote:


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 )


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
Jul 18 '05 #6
On Tue, 30 Nov 2004 21:54:46 GMT, bo**@oz.net (Bengt Richter) wrote:
[...]

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
Jul 18 '05 #7
"anton muhin" wrote:
Stefan Behnel wrote:


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
Jul 18 '05 #8
On Wed, 1 Dec 2004 10:23:28 -0500, Dave Merrill <dm*******@usaq.netq> wrote:
"anton muhin" wrote:
Or dict((key, row[key]) for key in cols).


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.


They use generator expressions, which were introduced by Python 2.4.
See <http://www.python.org/dev/doc/devel/whatsnew/node4.html>.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 18 '05 #9

The correct syntax is:

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

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

/Jean Brouwers

In article <7O********************@rcn.net>, Dave Merrill
<dm*******@usaq.netq> wrote:
"anton muhin" wrote:
Stefan Behnel wrote:


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

Jul 18 '05 #10
Dave Merrill wrote:
"anton muhin" wrote:
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
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Tuang | last post: by
16 posts views Thread by Daniel Klein | last post: by
1 post views Thread by garett | last post: by
2 posts views Thread by Steve | last post: by
12 posts views Thread by Donnal Walter | last post: by
31 posts views Thread by Brian Sabbey | last post: by
20 posts views Thread by W Karas | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.