Count How Many Items Are in a Python List

When working with lists in Python something we need to know how many items are in the list before proceeding, and this is what we will show today.

Lists

A Python list (also known as an array) holds items (numbers or strings) in a structure that allows us to access them by the order they were inserted into the list.

Sometimes we need to count the number of items before proceeding with the program.

The following example shows how to print how many items are in the list using the len() function.


mylist = ["item1", "item2"]
print (len(mylist))

We could also print same the number of items in the list to a variable and print it.

x = len(mylist)
print(x)

Leave a comment

Your email address will not be published. Required fields are marked *