Python Forum
Equivalent in List, of the "len(string)"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Equivalent in List, of the "len(string)"
#1
Hi all! Which is the keyword that makes python count the number of items in a List ??
Reply
#2
You can get the length of the list: len(object)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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.)
Reply
#4
(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 :)
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,613 Jan-05-2024, 08:30 PM
Last Post: sgrey
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,236 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  equivalent commands saeed_balk 2 2,664 Sep-23-2018, 05:44 AM
Last Post: saeed_balk
  Create Alert if string from list appears on other list javalava 1 2,499 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,310 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,519 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020