Syntax for extracting multiple items from a dictionary

Discussion in 'Python' started by shark, Nov 30, 2004.

  1. shark

    shark Guest

    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
    shark, Nov 30, 2004
    #1
    1. Advertising

  2. shark

    Roy Smith Guest

    In article <>, "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"}
    >
    > 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]
    Roy Smith, Nov 30, 2004
    #2
    1. Advertising

  3. 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?
    Leif K-Brooks, Nov 30, 2004
    #3
  4. 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
    Stefan Behnel, Nov 30, 2004
    #4
  5. shark

    anton muhin Guest

    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.
    anton muhin, Nov 30, 2004
    #5
  6. On Tue, 30 Nov 2004 15:20:19 +0100, Stefan Behnel <-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
    Bengt Richter, Nov 30, 2004
    #6
  7. On Tue, 30 Nov 2004 21:54:46 GMT, (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
    Bengt Richter, Nov 30, 2004
    #7
  8. shark

    Dave Merrill Guest

    "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
    Dave Merrill, Dec 1, 2004
    #8
  9. On Wed, 1 Dec 2004 10:23:28 -0500, 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. 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,
    ,
    http://www.brunningonline.net/simon/blog/
    Simon Brunning, Dec 1, 2004
    #9
  10. The correct syntax is:

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

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

    /Jean Brouwers



    In article <>, Dave Merrill
    <> 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
    >
    >
    Jean Brouwers, Dec 1, 2004
    #10
  11. shark

    Peter Hansen Guest

    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
    Peter Hansen, Dec 1, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Klein

    "Extracting" a dictionary

    Daniel Klein, May 17, 2004, in forum: Python
    Replies:
    16
    Views:
    484
    Noldoaran
    May 21, 2004
  2. Ilias Lazaridis
    Replies:
    6
    Views:
    433
    Ilias Lazaridis
    Feb 21, 2006
  3. Replies:
    2
    Views:
    236
    Jyotirmoy Bhattacharya
    Jul 5, 2007
  4. vsoler
    Replies:
    5
    Views:
    206
    Patrick Maupin
    Apr 13, 2010
  5. ela
    Replies:
    12
    Views:
    343
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page