5

What's the best way of counting the number of elements in a iterable (through iteration), without storing the elements in memory?

Right now, I'm just doing sum(1 for x in iterable). I was hoping to find something in itertools, but there doesn't seem to be anything.

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
  • 1
    As a sidenote, this feature has been requested years back: https://mail.python.org/pipermail/python-list/2003-August/182237.html, but unfortunately the discussion stopped there, I think this question comes up so often it should be added to python. – simonzack Sep 18 '14 at 10:39

2 Answers2

5

sum(1 for x in iterable) is the best method. Not everything requires a dedicated itertools function. :-) I note that the top-voted answer on the post you claim not to be a dupe of also advices you to use this.

Of course, it is always valuable to look at itertools just in case, and if you do, don't forget to check the recipes section; you'll find the quantify() recipe which does pretty much the same thing, but with a predicate to filter the iterable:

def quantify(iterable, pred=bool):
    "Count how many times the predicate is true"
    return sum(imap(pred, iterable))
Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

IMO this feature should be included in itertools, but is not.

more-itertools is a package which includes ilen, but could be a little heavy-weight for your situation. When I encounter this I just define your commonly used workaround as ilen:

def ilen(iterable):
    return sum(1 for _ in iterable)
simonzack
  • 19,729
  • 13
  • 73
  • 118
  • 1
    I don't think there is much value in taking the *basic building blocks* that `itertools` provide and wrapping something as simple as that one-liner into a function. Because the `ilen()` function in `more-itertools` does exactly what you included in your answer here. – Martijn Pieters Sep 18 '14 at 10:53
  • @MartijnPieters It can be useful if it's used often enough, as the `sum` workaround doesn't feel that clean to me. – simonzack Sep 18 '14 at 10:56
  • `more-itertools` seems to include lots of useful methods, I wish I had known about it sooner! – loopbackbee Sep 18 '14 at 12:46
  • @simonzack note that [you're using the `sum` workaround](https://github.com/erikrose/more-itertools/blob/master/more_itertools/more.py#L203) when you use `ilen`, which is what Martijn Pieters was referring to, I think. `ilen` makes your intention much more clear, though. – loopbackbee Sep 18 '14 at 12:47