Python Count Number Of Elements In List – Full Guide

Python lists are built-in datatype used to store multiple items in a single variable or in other words a collection of data.

You can count the number of elements in a list in python using the len(list) function.

This tutorial teaches you the different methods to count the number of elements in a list.

If You’re in Hurry…

You can use the below code snippet to get the number of items in the List.


list = ['a','b','c'] 

len(list)

You’ll see the output 3 as the list contains 3 items.

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods and use cases to count the number of items available in the list.

Using Len() Function

You can find the length of the list using the len() function.

Snippet


list = ['a','b','c']

len(list)

There are 3 elements in the list.

Output

You’ll see the output as 3.

3

You’ve calculated the items in the list which have the same type of values.

This is how you can get the number of elements in the list using the len() function.

Using For Loop

In this section, you’ll learn how to count the number of elements in a list using the for loop.

for loop is used to iterate over a sequence of values.

To get the number of elements in the list,

  • you’ll iterate over the list and increment the counter variable during each iteration.
  • Once the iteration is over, you’ll return the count variable, which has the total number of elements in the list.

Snippet

In the below example,

  • You’ve initialized a list with different types of values
  • Created a function which will iterate the list and count the elements
  • Printed the count using a print statement.
list = ['a',1, True, None]

def get_no_of_elements(list):
    count = 0
    for element in list:
        count += 1
    return count

print("Number of elements in the list: ", get_no_of_elements(list))

There are 4 elements in the list including the None value. Hence you’ll see output 4.

Output

Number of elements in the list:  4

This is how you can get the number of elements in a list using for loop.

Count Number of Elements In List Matching Criteria

In this section, you’ll learn how to count the number of elements in a list that is matching criteria or a within a specified condition.

  • First, create a function that will check if an item matches the condition.
  • For example, if the number is greater than 10. The function will return True if the condition is passed. Else it’ll return False.
  • Execute the function for each item in the list using the list comprehension.
  • Finally, you can sum the results where the condition is True.

Snippet

# Define any condition here
def condition(x):

    return x > 10

# Create the list
list = [10, 15, 25, 28, 3, 5, 8]

# Count the number of matching elements
print(sum(condition(x) for x in list))

Where,

  • condition(x) for x in list – List comprehension to apply the condition for each item in list.

Output

The sample list has 3 items that are greater than 10. Hence you’ll see output 3.

    3

This is how you can get the number of items in the list matching criteria.

Count None In List

In this section, you’ll learn how to count None in list.

You can count None in a list using the list comprehension method.

In list comprehension, you’ll iterate through the list and sum the None value.

list = ['one','two', 'three', None, None,'Six', None, 'Eight']

print("Number of None is List : ", sum(x is None for x in list))

Output

Number of None is List :  3

This is how you can count None in the List.

Conclusion

To summarize, you’ve learned how to get the number of elements in the list. You’ve used the len() function and for loops to count the number of elements in the list. You’ve also learned how to find list length.

If you have any questions, comment below.

You May Also Like

Leave a Comment