How do you increment through a for loop by 2 or more using Python?
To iterate by 2 or more when processing a for loop expression use the third parameter of the
range(start, stop, step)
built-in function, or if using the slice operator use the third parameter.
Here is an example demonstrating how to iterate through a for-loop in Python using the
range()
function.
>>> for i in range(0, 10, 1):
... print(i)
...
0
1
2
3
4
5
6
7
8
9
As you can see from the above code the
range()
function takes three parameters: start, stop and step.
The first parameter
start
is optional and sets where to start in the for loop, with
0
being the default value (the first item in the loop).
The second parameter
stop
is a required field and sets the index position to stop iterating through the object and
excludes
that item from the iteration process. In the example above notice how the iteration stopped at the number 9 and did not include the number 10.
The last optional parameter is the
step
parameter and this sets the frequency of jump to the next item. If this parameter isn’t set the default jump is
1
.
It’s this last parameter that helps us to be able to increment through the for loop at different rates.
Here’s an example where I’m looping through each second element:
>>> for i in range(0, 10, 2):
... print(i)
...
0
2
4
6
8
As you can see from the above code by changing the
step
parameter to a different value, such as
2
, this allows me to loop faster and only deal with every second element.
In the example above this allowed me get each second number starting from zero and ending at 10 (excluding 10).
Alternatively, you could change this step value to be 3 or 4, it really just depends upon your need.
>>> for i in range(0, 10, 3)
... print(i)
...
0
3
6
9
Iterate By 2 Or More Using Slice Operator
Applying the same logic you can also use the slice operator if you are working on a list. This means you don’t even have to use a for loop to iterate if your sole objective is to get every nth item from the original list.
For example, if you want to only fetch every second element from a list then you could easily achieve this by using the handy slice operator as shown below:
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[::2]
[1, 3, 5]
As you can see from the above example the slice operator also has three parameters except instead of being separated by commas in a standard function call they are separated by colons.
The same definitions for each parameter in the
range()
function equally apply here with the slice operator
[start:stop:step]
. If
start
isn’t defined it assumes from the very first element, and if
stop
isn’t defined then it assumes going to the very end, with
step
if not defined being a jump of 1.
Therefore, if you need to extract every second element from a list simply use the slice operator
my_list[::2]
where
my_list
represents the name of the variable containing your list.
Summary
To iterate through a for loop at a jump of 2 or more use the
range()
function and set the third parameter in this function to the value of 2 or whatever iteration you need.
Similarly, if you are working with a list you may not need to even use a for loop if you simply want to extract every second element from your list. If this is your case then use the slice operator
my_list[::2]
where
my_list
represents the name of the variable referencing your list value.