Count Number of Integers in List in Python (2 Examples)


In this Python tutorial, we will show examples of counting the number of integers in a list.

The content is structured as follows:

Let’s go straight into the code!


Creation of the Sample Data

First, we will need to define some data to use in the examples.

my_list = ['blue', 3, 5, 'orange', 4, 'green', 9, 15, 33]

As seen in the previous output, our data my_list is a list containing three strings and six integers. Let’s see now how to count the number of integers in it!


Example 1: Count the Number of Integers in a List Using a for Loop

In this first example, we will explain how to use a for loop to count the number of integers in our list. As you can see in the Python code below, we iterate over the elements in our list by checking if the element is an integer via the is keyword and the type() function, then storing the boolean outputs in a new list called new_list using the append() method. In the final step, the len() function helps us to count how many elements are in new_list.

new_list = []
for i in my_list:
    if(type(i) is int):
# 6

The Python output shows that my_list contains six integers.


Example 2: Count the Number of Integers in a List Using a List Comprehension

It’s also possible to count the number of integers in a list using the list comprehension technique using the len() and isinstance() functions. For the implementation, we need to specify in the isinstance() function that we are looking for integers in our list, using int input for the classinfo parameter.

len(list(i for i in my_list if isinstance(i, int)))
# 6

The printed count is 6, the same as Example 1.


Video, Further Resources & Summary

Do you need more explanations on how to count the number of integers in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.


The YouTube video will be added soon.


Furthermore, you may have a look at the related articles on our website:

This post has shown how to count the number of integers in a list in Python. You can write in the comments section below in case you have additional questions.


Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.