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

y2k picture y2k · Nov 11, 2009 · Viewed 3.3M times · Source

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

gnud picture gnud · Nov 11, 2009

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()