How to Count Number of Elements in a List in Python?
Hi Dev,
I am going to explain you example of python count number of items in list. I explained simply about how to count number of elements in a list in python. you can see python count all elements in a list. you'll learn python count elements in list of lists. you will do the following things for python list count all elements.
There are many ways to find length of list in python. i will give you two examples using len() method and for loop to count all elements of list in python. so let's see the below examples.
You can use these examples with python3 (Python 3) version.
let's see below a simple example with output:
Example 1: using len() Method
main.py
myList = [10, 5, 23, 25, 14, 80, 71] # Count number of element to list count = len(myList) print(count)
Output:
7
Example 2: using For Loop
main.py
myList = [10, 5, 23, 25, 14, 80, 71] # Count number of element to list def getNumberOfElement(list): count = 0 for element in list: count += 1 return count count = getNumberOfElement(myList) print(count)
Output:
7
I hope it can help you...