RE: for in sequence problem... possible new operator to add to python

Discussion in 'Python' started by sismex01@hebmex.com, Jul 10, 2003.

  1. Guest

    > From: Peter Hansen [mailto:p]
    > Sent: Jueves, 10 de Julio de 2003 11:36 a.m.
    >
    > Adam Gent wrote:
    > > [..snippage..]

    >
    > [..more.snippage..]
    >
    > When you iterate over a dict in recent versions of Python, you
    > are by definition iterating over the keys in the dict. If you
    > want the values, you use .values(), and if you want both keys
    > and values, you use .items(). See the docs for more.
    >
    > -Peter
    >


    Actually, .keys(), .values() and .items() return their respective
    lists, in arbitrary order.

    If you wish to use an iterator, use .iterkeys() , .itervalues()
    or .iteritems() ; very helpful in the case of big dictionaries,
    since you don't need to create and then destroy big lists.

    Adam: If you wish to iterate through the items by default,
    have you tried something like this?

    class NewDict(dict):
    def __iter__(self):
    return super(NewDict,self).itervalues()

    That should, by default (like "for x in nd:") iterate through
    the values of a dictionary, instead of it's keys.

    I haven't tested it though, so caveat emptor.

    -gustavo
    Advertencia:La informacion contenida en este mensaje es confidencial y
    restringida, por lo tanto esta destinada unicamente para el uso de la
    persona arriba indicada, se le notifica que esta prohibida la difusion de
    este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
    la transmision, favor de comunicarse con el remitente. Gracias.
     
    , Jul 10, 2003
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    >
    > > From: Peter Hansen [mailto:p]
    > > When you iterate over a dict in recent versions of Python, you
    > > are by definition iterating over the keys in the dict. If you
    > > want the values, you use .values(), and if you want both keys
    > > and values, you use .items(). See the docs for more.

    >
    > Actually, .keys(), .values() and .items() return their respective
    > lists, in arbitrary order.


    I realize that. I didn't mean to imply anything different.

    > If you wish to use an iterator, use .iterkeys() , .itervalues()
    > or .iteritems() ; very helpful in the case of big dictionaries,
    > since you don't need to create and then destroy big lists.


    Sorry, perhaps I should stop using the term "iterate" for its
    more widely known generic meaning of visiting each item in a
    sequence one at a time, and restrict my usage only to those
    cases where in Python I'm talking about an actual "iterator"
    object.

    ("Iterate" was a very general term... it would be a shame if
    one could no longer use it as such.)

    -Peter
     
    Peter Hansen, Jul 10, 2003
    #2
    1. Advertising

  3. John J. Lee Guest

    Peter Hansen <> writes:

    > wrote:
    > >
    > > > From: Peter Hansen [mailto:p]
    > > > When you iterate over a dict in recent versions of Python, you

    [...]
    > Sorry, perhaps I should stop using the term "iterate" for its
    > more widely known generic meaning of visiting each item in a
    > sequence one at a time, and restrict my usage only to those
    > cases where in Python I'm talking about an actual "iterator"
    > object.
    >
    > ("Iterate" was a very general term... it would be a shame if
    > one could no longer use it as such.)


    I think you're correct even in the strict Python sense of the word
    'iteration'. Objects (eg. dicts and lists) that are not themselves
    iterators can still support iteration. Section 2.2.5 from the 2.2
    library reference:

    Python defines several iterator objects to support iteration over
    general and specific sequence types, dictionaries, and other more
    specialized forms. ...


    John
     
    John J. Lee, Jul 11, 2003
    #3
    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. Paul Porcelli
    Replies:
    1
    Views:
    465
    Chris Charley
    Jul 14, 2004
  2. Paul Porcelli
    Replies:
    3
    Views:
    526
    Eric Amick
    Jul 16, 2004
  3. stef mientki
    Replies:
    13
    Views:
    673
    stef mientki
    Oct 20, 2007
  4. xmllmx
    Replies:
    6
    Views:
    434
    xmllmx
    Feb 3, 2010
  5. Paul Porcelli

    Problem with Range Operator sequence number

    Paul Porcelli, Jul 14, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    130
    Sandman
    Jul 15, 2004
Loading...

Share This Page