Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Why is len() always return 1 for list

Not sure why len() is returning just 1 as output in Python. Should be 4.

names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
num_items = len(names)
print(num_items,",",names,type(names))

Output

Give me everybody's names, separated by a comma. angel,barbara,mary,jjjj
1, ['angel,barbara,mary,jjjj'] <class 'list'>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your input string ('angel,barbara,mary,jjjj') names are separated with commas without spaces (','), but you try to split it by comma and space (', '). There is no ', ' in the string, so it’s not split

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading