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

Shane Geiger sgeiger at ncee.net
Wed Mar 14 17:54:55 CET 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
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070314/19a703aa/attachment.vcf>


More information about the Python-list mailing list