0
Please what's the main function of *len* in python
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
+ 4
It's used to determine how many elements are in an iterable, for example a list, dict or set.
0
Thanks for the quick response đđ