On Sun, Dec 2, 2018 at 11:08 PM Morten W. Petersen <morp...@gmail.com> wrote: > > On Sun, Dec 2, 2018 at 12:49 PM Chris Angelico <ros...@gmail.com> wrote: >> To my knowledge, len(x) == len(list(x)) for any core data type that >> has a length. > > >>> len(range(0,100,3)) > 34 > >>> range(0,100,3).__len__ > <method-wrapper '__len__' of range object at 0x7f144d5e3210> > >>> range(0,100,3).__len__() > 34 > >>> > > But again, I guess an integer-to-be-calculated would be suitable > for iterators where it is very expensive to do a list or other > similar calculation. > > These operations take quite a bit of time: > > >>> x=list(range(0,100000000,3)) > >>> y=len(list(range(0,100000000,3))) > >>> >
Of course they take a lot of time - you're asking for the generation of some thirty-three million integer objects, each one individually built and refcounted (in CPython), just so you can count them. But my point about the equivalency is that, rather than calculating len(list(range(...))), you can calculate len(range(...)), and you can be confident that you WILL get the same result. ChrisA -- https://mail.python.org/mailman/listinfo/python-list