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

2007-03-15 Thread Duncan Booth
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, 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

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

2007-03-15 Thread bearophileHUGS
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 -- http://mail.python.org/mailman/listinfo

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

2007-03-15 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, 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 -- http://mail.python.org/mailman/listinfo/python-list

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

2007-03-15 Thread skip
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 -- http://mail.python.org/mailm

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

2007-03-15 Thread Duncan Booth
[EMAIL PROTECTED] (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

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

2007-03-15 Thread Alex Martelli
Steve Holden <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] 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,

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

2007-03-15 Thread bearophileHUGS
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 -- http://mail.python.org/mailm

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

2007-03-15 Thread Bruno Desthuilliers
Steve Holden a écrit : > [EMAIL PROTECTED] 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:

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

2007-03-15 Thread Steve Holden
[EMAIL PROTECTED] 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): >

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

2007-03-15 Thread bearophileHUGS
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__"):

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

2007-03-15 Thread Laurent Pointal
Paul Rubin a écrit : > Laurent Pointal <[EMAIL PROTECTED]> 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 man

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

2007-03-14 Thread Paul Rubin
Laurent Pointal <[EMAIL PROTECTED]> 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 contai

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

2007-03-14 Thread skip
>> I find "iter" 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

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

2007-03-14 Thread Drew
On Mar 14, 2:53 pm, [EMAIL PROTECTED] 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" to be extremely ugly and hope to avoid using them >

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

2007-03-14 Thread skip
>> 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" to be extremely ugly and hope to avoid using them altogether until they are gone in Py3k. Skip -- http:/

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

2007-03-14 Thread Leif K-Brooks
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. xra

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

2007-03-14 Thread Shane Geiger
# 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

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

2007-03-14 Thread Drew
On Mar 14, 11:44 am, Laurent Pointal <[EMAIL PROTECTED]> 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

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

2007-03-14 Thread Laurent Pointal
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 m

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

2007-03-14 Thread Drew
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() essential