Friday, June 22, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...