We've seen how lists hold collections of items, and how we can add (.append()
) or remove (.remove()
) items.
Engagement Message
But how can we easily find out exactly how many items are currently inside a list?
Every list has a length (or size), which is simply the count of items it contains.
A list like [10, 20, 30]
has three items. An empty list []
has zero items.
Engagement Message
Does the length change when we append or remove items?
Python provides a built-in function called len()
to get the length of a list. It's kind of like print()
but only works on lists and returns the length.
You use it like this: len(your_list_variable)
. It takes the list as input inside the parentheses.
Engagement Message
What kind of value do you expect len()
to give back? A string, a number, or something else?
The len()
function returns an integer representing the number of items.
If colors = ["red", "green", "blue"]
, then would store the integer in the variable.
