Python Forum
Equivalent in List, of the "len(string)" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Equivalent in List, of the "len(string)" (/thread-11609.html)



Equivalent in List, of the "len(string)" - sylas - Jul-18-2018

Hi all! Which is the keyword that makes python count the number of items in a List ??


RE: Equivalent in List, of the "len(string)" - wavic - Jul-18-2018

You can get the length of the list: len(object)


RE: Equivalent in List, of the "len(string)" - micseydel - Jul-18-2018

(Jul-18-2018, 05:23 AM)sylas Wrote: Hi all! Which is the keyword that makes python count the number of items in a List ??
To clarify, "keyword" has a technical meaning in Python, and includes things like return, yield and in Python 2, print. However, len is a built-in function. The difference between the two can be subtle, but programming is a technical discipline and your post count is high enough that don't want anyone stumbling across this in the future to misread what they see here.

(wavic's answer is obviously the useful answer, but I'm feeling pedantic I guess.)


RE: Equivalent in List, of the "len(string)" - nilamo - Jul-19-2018

(Jul-18-2018, 05:23 AM)sylas Wrote: Hi all! Which is the keyword that makes python count the number of items in a List ??

len(string) isn't a thing, as is implied by the subject. It's len(iterable). Strings, lists, dicts, ranges, etc are iterables, and len works with them just fine :)


RE: Equivalent in List, of the "len(string)" - micseydel - Jul-19-2018

(Jul-19-2018, 09:01 PM)nilamo Wrote: It's len(iterable). Strings, lists, dicts, ranges, etc are iterables, and len works with them just fine :)
I almost said this, but it's not always true -
Output:
>>> len(x for x in range(0)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'generator' has no len()
Generators are iterable, but you can't get their length. You'd have to turn them into a collection (list or tuple in this case) to get their length, but some generators have infinite length.