Python - Get Number of Items in a List

1 min read

In Python you can use the built-in len() function to get the number of elements in a list.

fruit = ['apple', 'pear', 'banana', 'kiwi']
num_fruit = len(fruit)
print(num_fruit) # 4

You can also use a list's count() method if you want to get the number of a specific item.

fruit = ['apple', 'peach', 'lemon', 'apple']
num_apples = fruit.count('apple') print(num_apples) # 2

Author picture
Rhett Trickett

Founder at Able

@rhett RhettTrickett rhetttrickett

Join the discussion

Able is a developer community where people build their coding knowledge and careers.

Join with GitHub Join with Twitter