dict.items() vs dict.iteritems and similar questions

Discussion in 'Python' started by Drew, Mar 14, 2007.

  1. Drew

    Drew Guest

    When is it appropriate to use dict.items() vs dict.iteritems. Both
    seem to work for something like:

    for key,val in mydict.items():
    print key,val

    for key,val in mydict.iteritems():
    print key,val

    Also, when is it appropriate to use range() vs xrange(). From my
    understanding, xrange() essentially gives you an iterator across a
    range, so it should be used when iterating. Should you only use
    range() when want to physically store the range as a list?

    Thanks,
    Drew
    Drew, Mar 14, 2007
    #1
    1. Advertising

  2. Drew a écrit :
    > When is it appropriate to use dict.items() vs dict.iteritems. Both
    > seem to work for something like:
    >
    > for key,val in mydict.items():
    > print key,val
    >
    > for key,val in mydict.iteritems():
    > print key,val
    >
    > Also, when is it appropriate to use range() vs xrange(). From my
    > understanding, xrange() essentially gives you an iterator across a
    > range, so it should be used when iterating. Should you only use
    > range() when want to physically store the range as a list?


    iteritems and xrange only provide values when requested.
    items and range build complete list when called.

    Both work, you may prefer xrange/iteritems for iteration on large
    collections, you may prefer range/items when processing of the result
    value explicitly need a list (ex. calculate its length) or when you are
    going to manipulate the original container in the loop.

    A+

    Laurent.
    Laurent Pointal, Mar 14, 2007
    #2
    1. Advertising

  3. Drew

    Drew Guest

    On Mar 14, 11:44 am, Laurent Pointal <> wrote:
    > Both work, you may prefer xrange/iteritems for iteration on large
    > collections, you may prefer range/items when processing of the result
    > value explicitly need a list (ex. calculate its length) or when you are
    > going to manipulate the original container in the loop.
    >
    > A+
    >
    > Laurent.


    Laurent -

    Extremely helpful, exactly what I was looking for.

    Thanks,
    Drew
    Drew, Mar 14, 2007
    #3
  4. Drew

    Shane Geiger Guest

    # Just by looking at the output, it seems pretty obvious that xrange
    would be more memory effcient for large ranges:

    print "With range():",range(100,200)
    print
    print "With xrange():",xrange(100,200)

    d = {1:2,2:3,3:4}
    d.items()
    d.iteritems()

    # I have been curious to use Pysizer (which requires patching Python)
    to demonstrate the difference.



    Drew wrote:
    > When is it appropriate to use dict.items() vs dict.iteritems. Both
    > seem to work for something like:
    >
    > for key,val in mydict.items():
    > print key,val
    >
    > for key,val in mydict.iteritems():
    > print key,val
    >
    > Also, when is it appropriate to use range() vs xrange(). From my
    > understanding, xrange() essentially gives you an iterator across a
    > range, so it should be used when iterating. Should you only use
    > range() when want to physically store the range as a list?
    >
    > Thanks,
    > Drew
    >
    >


    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
    Shane Geiger, Mar 14, 2007
    #4
  5. Laurent Pointal wrote:
    > Both work, you may prefer xrange/iteritems for iteration on large
    > collections, you may prefer range/items when processing of the result
    > value explicitly need a list (ex. calculate its length) or when you are
    > going to manipulate the original container in the loop.


    xrange actually supports len():

    >>> len(xrange(10))

    10
    Leif K-Brooks, Mar 14, 2007
    #5
  6. Drew

    Guest

    >> When is it appropriate to use dict.items() vs dict.iteritems.

    Laurent> Both work, you may prefer xrange/iteritems for iteration on
    Laurent> large collections...

    I find "iter<anything>" to be extremely ugly and hope to avoid using them
    altogether until they are gone in Py3k.

    Skip
    , Mar 14, 2007
    #6
  7. Drew

    Drew Guest

    On Mar 14, 2:53 pm, wrote:
    > >> When is it appropriate to use dict.items() vs dict.iteritems.

    >
    > Laurent> Both work, you may prefer xrange/iteritems for iteration on
    > Laurent> large collections...
    >
    > I find "iter<anything>" to be extremely ugly and hope to avoid using them
    > altogether until they are gone in Py3k.
    >
    > Skip


    Skip -

    Ugly, maybe, but don't you take a decent performance hit when loading
    the entire dict into memory at once? Especially if the dict is large?
    Drew, Mar 14, 2007
    #7
  8. Drew

    Guest

    >> I find "iter<anything>" to be extremely ugly and hope to avoid using
    >> them altogether until they are gone in Py3k.


    Drew> Ugly, maybe, but don't you take a decent performance hit when
    Drew> loading the entire dict into memory at once? Especially if the
    Drew> dict is large?

    Sure, but I try hard to keep my dicts small. ;-)

    Skip
    , Mar 14, 2007
    #8
  9. Drew

    Paul Rubin Guest

    Laurent Pointal <> writes:
    > Both work, you may prefer xrange/iteritems for iteration on large
    > collections, you may prefer range/items when processing of the result
    > value explicitly need a list (ex. calculate its length) or when you are
    > going to manipulate the original container in the loop.


    You can use len(d) if d is a dict.
    Paul Rubin, Mar 14, 2007
    #9
  10. Paul Rubin a écrit :
    > Laurent Pointal <> writes:
    >> Both work, you may prefer xrange/iteritems for iteration on large
    >> collections, you may prefer range/items when processing of the result
    >> value explicitly need a list (ex. calculate its length) or when you are
    >> going to manipulate the original container in the loop.

    >
    > You can use len(d) if d is a dict.


    Yes, as long as you have a semantic relation between the original dict
    and the extracted keys. But once you have extracted the keys, and pass
    them around your functions, if you miss the relationship, you have
    either a list container or a generator. Not considering the case where
    original dict is modified between keys extraction and keys usage...

    But as we dont know more about OP needs...


    A+

    Laurent.
    Laurent Pointal, Mar 15, 2007
    #10
  11. Drew

    Guest

    Laurent Pointal:
    > you may prefer range/items when processing of the result
    > value explicitly need a list (ex. calculate its length)


    Creating a very long list just to know the len of an iterator is
    barbaric, so sometimes I use this:

    def leniter(iterator):
    if hasattr(iterator, "__len__"):
    return len(iterator)
    nelements = 0
    for _ in iterator:
    nelements += 1
    return nelements

    Bye,
    bearophile
    , Mar 15, 2007
    #11
  12. Drew

    Steve Holden Guest

    wrote:
    > Laurent Pointal:
    >> you may prefer range/items when processing of the result
    >> value explicitly need a list (ex. calculate its length)

    >
    > Creating a very long list just to know the len of an iterator is
    > barbaric, so sometimes I use this:
    >
    > def leniter(iterator):
    > if hasattr(iterator, "__len__"):
    > return len(iterator)
    > nelements = 0
    > for _ in iterator:
    > nelements += 1
    > return nelements
    >

    Of course this is a little like the Heisenberg uncertainty principle if
    the iterator has no __len__ attribute - once you know how long it is you
    no longer have access to the elements. Or did I miss something?

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
    Steve Holden, Mar 15, 2007
    #12
  13. Steve Holden a écrit :
    > wrote:
    >> Laurent Pointal:
    >>> you may prefer range/items when processing of the result
    >>> value explicitly need a list (ex. calculate its length)

    >>
    >> Creating a very long list just to know the len of an iterator is
    >> barbaric, so sometimes I use this:
    >>
    >> def leniter(iterator):
    >> if hasattr(iterator, "__len__"):
    >> return len(iterator)
    >> nelements = 0
    >> for _ in iterator:
    >> nelements += 1
    >> return nelements
    >>

    > Of course this is a little like the Heisenberg uncertainty principle if
    > the iterator has no __len__ attribute - once you know how long it is you
    > no longer have access to the elements. Or did I miss something?


    yes, that's one of the side effects. Another interesting case:

    import itertools
    it = itertools.cycle(range(10))
    print "it has %d elements" % leniter(it)
    Bruno Desthuilliers, Mar 15, 2007
    #13
  14. Drew

    Guest

    Steve Holden:
    > once you know how long it is you
    > no longer have access to the elements. Or did I miss something?


    Now and then I need to know how many elements there are, and not what
    they are, so in those situations storing them isn't necessary.

    Bye,
    bearophile
    , Mar 15, 2007
    #14
  15. Steve Holden <> wrote:

    > wrote:
    > > Laurent Pointal:
    > >> you may prefer range/items when processing of the result
    > >> value explicitly need a list (ex. calculate its length)

    > >
    > > Creating a very long list just to know the len of an iterator is
    > > barbaric, so sometimes I use this:
    > >
    > > def leniter(iterator):
    > > if hasattr(iterator, "__len__"):
    > > return len(iterator)
    > > nelements = 0
    > > for _ in iterator:
    > > nelements += 1
    > > return nelements
    > >

    > Of course this is a little like the Heisenberg uncertainty principle if
    > the iterator has no __len__ attribute - once you know how long it is you
    > no longer have access to the elements. Or did I miss something?


    Right. However, "return sum(1 for _ in iterator)" may be a handier way
    to express the same desctructive semantics as the last 4 lines here.


    Alex
    Alex Martelli, Mar 15, 2007
    #15
  16. Drew

    Duncan Booth Guest

    (Alex Martelli) wrote:

    >> Of course this is a little like the Heisenberg uncertainty principle if
    >> the iterator has no __len__ attribute - once you know how long it is you
    >> no longer have access to the elements. Or did I miss something?

    >
    > Right. However, "return sum(1 for _ in iterator)" may be a handier way
    > to express the same desctructive semantics as the last 4 lines here.


    I think I'd prefer the barbaric:

    return len(list(iterator))

    since at least it is guaranteed to terminate.
    Duncan Booth, Mar 15, 2007
    #16
  17. Drew

    Guest

    Duncan> I think I'd prefer the barbaric:

    Duncan> return len(list(iterator))

    Duncan> since at least it is guaranteed to terminate.

    Are you sure? There's no guarantee that an iterator will terminate:

    len(list(itertools.cycle(range(10))))

    Skip
    , Mar 15, 2007
    #17
  18. In <>, skip wrote:

    > Are you sure? There's no guarantee that an iterator will terminate:
    >
    > len(list(itertools.cycle(range(10))))


    You have infinite memory? ;-)

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Mar 15, 2007
    #18
  19. Drew

    Guest

    Alex Martelli:

    > Right. However, "return sum(1 for _ in iterator)" may be a handier way
    > to express the same desctructive semantics as the last 4 lines here.


    With the speed tests I have done my version did come out as the faster
    one.

    Bye,
    bearophile
    , Mar 15, 2007
    #19
  20. Drew

    Duncan Booth Guest

    Marc 'BlackJack' Rintsch <> wrote:

    > In <>, skip wrote:
    >
    >> Are you sure? There's no guarantee that an iterator will terminate:
    >>
    >> len(list(itertools.cycle(range(10))))

    >
    > You have infinite memory? ;-)


    Strangely, Skip's example is exactly the one I tested before posting my
    claim that it would terminate.
    Duncan Booth, Mar 15, 2007
    #20
    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. Xah Lee

    iteritems() and enumerate()

    Xah Lee, Jan 20, 2005, in forum: Python
    Replies:
    4
    Views:
    14,240
    alex23
    Jan 21, 2005
  2. Henrik Goldman

    Merging a list of similar items

    Henrik Goldman, Jun 13, 2007, in forum: C++
    Replies:
    6
    Views:
    304
  3. rabad
    Replies:
    2
    Views:
    518
    Bruno Desthuilliers
    Jul 4, 2008
  4. Peng Yu
    Replies:
    8
    Views:
    350
    Steven D'Aprano
    Nov 20, 2009
  5. Patrick Sabin
    Replies:
    1
    Views:
    314
    Paul Rudin
    Nov 20, 2009
Loading...

Share This Page