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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Drew

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

    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.iteritem s():
    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

  • Laurent Pointal

    #2
    Re: dict.items() vs dict.iteritems and similar questions

    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.iteritem s():
    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.

    Comment

    • Drew

      #3
      Re: dict.items() vs dict.iteritems and similar questions

      On Mar 14, 11:44 am, Laurent Pointal <laurent.poin.. .@limsi.frwrote :
      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

      Comment

      • Shane Geiger

        #4
        Re: dict.items() vs dict.iteritems and similar questions

        # 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():",xran ge(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.iteritem s():
        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
        sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

        Leading the Campaign for Economic and Financial Literacy


        Comment

        • Leif K-Brooks

          #5
          Re: dict.items() vs dict.iteritems and similar questions

          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

          Comment

          • skip@pobox.com

            #6
            Re: dict.items() vs dict.iteritems and similar questions

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

            LaurentBoth work, you may prefer xrange/iteritems for iteration on
            Laurentlarge collections...

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

            Skip

            Comment

            • Drew

              #7
              Re: dict.items() vs dict.iteritems and similar questions

              On Mar 14, 2:53 pm, s...@pobox.com wrote:
              >When is it appropriate to use dict.items() vs dict.iteritems.
              >
              LaurentBoth work, you may prefer xrange/iteritems for iteration on
              Laurentlarge 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?

              Comment

              • skip@pobox.com

                #8
                Re: dict.items() vs dict.iteritems and similar questions

                >I find "iter<anything> " to be extremely ugly and hope to avoid using
                >them altogether until they are gone in Py3k.
                DrewUgly, maybe, but don't you take a decent performance hit when
                Drewloading the entire dict into memory at once? Especially if the
                Drewdict is large?

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

                Skip

                Comment

                • Paul Rubin

                  #9
                  Re: dict.items() vs dict.iteritems and similar questions

                  Laurent Pointal <laurent.pointa l@limsi.frwrite s:
                  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.

                  Comment

                  • Laurent Pointal

                    #10
                    Re: dict.items() vs dict.iteritems and similar questions

                    Paul Rubin a écrit :
                    Laurent Pointal <laurent.pointa l@limsi.frwrite s:
                    >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.

                    Comment

                    • bearophileHUGS@lycos.com

                      #11
                      Re: dict.items() vs dict.iteritems and similar questions

                      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(iterato r):
                      if hasattr(iterato r, "__len__"):
                      return len(iterator)
                      nelements = 0
                      for _ in iterator:
                      nelements += 1
                      return nelements

                      Bye,
                      bearophile

                      Comment

                      • Steve Holden

                        #12
                        Re: dict.items() vs dict.iteritems and similar questions

                        bearophileHUGS@ lycos.com 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(iterato r):
                        if hasattr(iterato r, "__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

                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: dict.items() vs dict.iteritems and similar questions

                          Steve Holden a écrit :
                          bearophileHUGS@ lycos.com 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(iterato r):
                          > if hasattr(iterato r, "__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)


                          Comment

                          • bearophileHUGS@lycos.com

                            #14
                            Re: dict.items() vs dict.iteritems and similar questions

                            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

                            Comment

                            • Alex Martelli

                              #15
                              Re: dict.items() vs dict.iteritems and similar questions

                              Steve Holden <steve@holdenwe b.comwrote:
                              bearophileHUGS@ lycos.com 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(iterato r):
                              if hasattr(iterato r, "__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

                              Comment

                              Working...