0

Please what's the main function of *len* in python

23rd Mar 2020, 7:47 PM
Joseph Nssien
4 Answers
+ 7
Just take note, not all iterables has len. def yrange(n): j = 0 while j < n: yield j j += 1 g = iter(yrange(10)) # g is a generator print(next(g)) # 0 print(next(g)) # 1 print(next(g)) # 2 print(len([g])) # 1 print(len(list(g))) #7 print(len(g)) # ERROR type generator has no len #https://docs.python.org/3.7/library/functions.html#len ''' len(s)¶ Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set). class list([iterable]) Rather than being a function, list is actually a mutable sequence type, as documented in Lists and Sequence Types — list, tuple, range ''' #https://anandology.com/python-practice-book/iterators.html https://code.sololearn.com/cKGJb1ItPpyS/?ref=app
23rd Mar 2020, 8:36 PM
Louis
Louis - avatar
+ 4
It's used to determine how many elements are in an iterable, for example a list, dict or set.
23rd Mar 2020, 7:51 PM
HonFu
HonFu - avatar
0
Thanks for the quick response 🙏🙏
23rd Mar 2020, 7:54 PM
Joseph Nssien