Friday 22 June 2018

python - How do I get the number of elements in a list?




Consider the following:



items = []
items.append("apple")
items.append("orange")
items.append("banana")

# FAKE METHOD:
items.amount() # Should return 3



How do I get the number of elements in the list items?


Answer



The len() function can be used with several different types in Python - both built-in types and library types. For example:



>>> len([1,2,3])
3


Official 2.x documentation is here: len()
Official 3.x documentation is here: len()



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...