Closed anitacaron closed 1 year ago
Hi @pkalita-lbl, is there a way of having the total number of elements returned when returning an Iterable?
There are a few different ways to get the length of an Iterable
. I usually reach for the functools.reduce
function:
import functools
iterable = ...
# starting with p = 0, compute p + 1 for each element
num_elements = functools.reduce(lambda prev, curr: prev + 1, iterable, 0)
Depending on exactly what type of Iterable
you're dealing with it might be worth checking for a __len__
property:
def count(iterable):
if hasattr(iterable, '__len__'):
return len(iterable)
return functools.reduce(lambda prev, curr: prev + 1, iterable, 0)
If you need to get the count without exhausting the original Iterable
(for example, if you need to return the first N elements and the count), itertools.tee
is useful:
import itertools
import functools
iterable = range(3000)
iter_1, iter_2 = itertools.tee(iterable)
result = {
'items': list(itertools.islice(iter_1, 10)),
'total': functools.reduce(lambda prev, curr: prev + 1, iter_2, 0),
}
# --> {'items': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'total': 3000}
Thank you, @pkalita-lbl!! I'll try the tee
function.
Also, add the page number and the total number of pages.