On 14/03/13 12:13, David Knupp wrote:
FWIW, if you're working with very large lists, but don't need to create the
full list in memory, then a generator expression is usually preferred. To get
the number of items a generator would return, you can use sum() like this:
gen = (n for n in xrange(some_really_huge_number))
sum(1 for n in gen) # outputs some_really_huge_number
Don't do that. You already know how many items you have, because you created
the xrange object:
count = some_really_huge_number
If you didn't create the xrange object yourself, or if you can't easily tell
how many items it will include, remember that xrange objects support the len
function:
py> x = xrange(23, 1987639292, 17)
py> len(x)
116919957
If you have some unknown, arbitrary iterable that doesn't support len(), then
you can use the sum() trick:
it = some_unknown_iterable()
sum(1 for x in it)
There is almost never any reason to write a list comprehension or generator
expression that merely walks over an iterable, without doing any further
processing:
(x for x in iterable)
is just like iterable, only slower because it is indirect.
--
Steven
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor