Count Number of Elements in a List in Python Example

26-Nov-2022

.

Admin

Count Number of Elements in a List in Python Example

Hi Dev,

This tutorial will provide example of count number of elements in a list in python example. if you have question about python count elements in list of lists then I will give simple example with solution. This article goes in detailed on python list count all elements. I explained simply step by step python count elements in list of lists. Alright, let’s dive into the steps.

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.

so let's see following examples 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

#Python