Python count items in the list

Previous
Next

In this tutorial, we will see how to count items inlist in python


Count total items in the list

You can use len() function to count items in the list.

Let’s understand with the help of example.

list1=[1,2,3,4] print("List:",list1) length=len(list1) print("Length of list1",length)

Output:

List: [1, 2, 3, 4]
Length of list1: 4

Count occurence of each element in the list

list1=[1,2,3,4,1,4,3,2,2,1,5] print("List:",list1) print("Count of 2 in list1:",list1.count(2))

Output:

List: [1, 2, 3, 4, 1, 4, 3, 2, 2, 1, 5]
Count of 2 in list1 3

That’s all about Python count items in the list.

Previous
Next

Add Comment