How to Count in Python Loop

This tutorial discusses about how to count in Python loop.

In Python, we have many objects that can be treated as iterable. An iterable is an object like a list, tuple, or any other sequence of elements that can be iterated using a simple for loop.

Counting in a loop means making a note of every iteration to find the total number of iterations after the loop has stopped running.

If you want to count in while loop, skip to count in while loop section.

1. Using the enumerate() Function

The enumerate() function takes an iterable and assigns a counter variable to each element. This way, every element gets assigned a value that can act as the index of the element.

We can use this index variable to act as a counter variable for the loop.

See the code below.

Output:

0 USA
1 Canada
2 France

In the above example, we display the index of the element after using the enumerate() function and iterating it. Note that the index starts from 0.

enumerate() function takes an iterables such as list as input and returns enumerate object containing a tuple where the first element is index and second element is iterable element.

enumerate() function also takes optional argument start. We will learn about it in next section.

2. Using the enumerate() Function with different start number

In case, you don’t want to start count with 0 and start it with different number such as 1, pass optional argument start argument with different value such as 1.

Let’s see with the help of example:

Output:

1 USA
2 Canada
3 France

2. Using a Counter Variable

This method is very straightforward, but manual counting method. We will declare a variable outside the loop and assign it a value of zero. In every iteration, we will increment its value and print it.

See the code below.

Output:

1
2
3

In the above example, we create a list lst that contains three elements. The variable c is initialized with zero. We use the for loop to iterate over this list. In every iteration, we increment the variable c and print its value.

You can start counter with different number if you want.

Output:

11
12
13

Here we start counter variable c with 10.

3. Using the zip() Function with range() function

The zip() function can be used to combine two iterables into one by combining the individual elements at corresponding positions together.

We can create a list of the length iterable with the range() function. The two can be then combined to create a new iterable. This method is just another way to emulate the enumerate() function discussed previously.

The code is demonstrated below.

Output:

0 USA
1 Canada
2 France

The range() function returns an object containing a sequence to the length of the list. This is combined with the list and the counter value is displayed.

4. Using range() function

This is similar to previous method, but here we will directly use range() function and will use index to print elements of the list.

Output:

0 USA
1 Canada
2 France

5. Count in while loop

To count in while loop

  • Define a variable named c with 0. You can initialize it 1 as well based on your requirements.
  • Iterate over while loop upto N.
  • In each iteration, increase the value by 1.
    1
    2
    3

6. Skip iteration while counting in the loop

To skip iteration while counting in the loop, you can use if clause with continue while iterating.
Here is simple example:

1
3

As you can see, when value of c was 1, we skipped the iteration by continuing the loop and that’s the reason we got 1 3 as output.

That’s all about how to count in Python loop.

Was this post helpful?

Leave a Reply

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