Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dict.items() vs dict.iteritems and similar questions

Reply
Thread Tools

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

 
 
Drew
Guest
Posts: n/a
 
      03-14-2007
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

 
Reply With Quote
 
 
 
 
Laurent Pointal
Guest
Posts: n/a
 
      03-14-2007
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.
 
Reply With Quote
 
 
 
 
Drew
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 11:44 am, Laurent Pointal <laurent.poin...@limsi.fr> 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

 
Reply With Quote
 
Shane Geiger
Guest
Posts: n/a
 
      03-14-2007
# 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


 
Reply With Quote
 
Leif K-Brooks
Guest
Posts: n/a
 
      03-14-2007
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
 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      03-14-2007
>> 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
 
Reply With Quote
 
Drew
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 2:53 pm, s...@pobox.com 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?

 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      03-14-2007
>> 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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-14-2007
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.
 
Reply With Quote
 
Laurent Pointal
Guest
Posts: n/a
 
      03-15-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any similar Webcam broadcasting site similar to youtube Chaudhry Nijjhar Computer Support 0 02-19-2008 11:48 PM
Could not load file or assembly 'App_Web_tod59ga8' and similar craigkenisston@hotmail.com ASP .Net 3 12-04-2005 06:15 PM
how are asp 3.0 and asp.net different/similar? Steve Richter ASP .Net 3 06-22-2005 10:59 PM
autoscan automake and similar and deprecated standard libraries classes Shalafi C++ 2 02-13-2005 11:17 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57