For Loop Increment By 2 in Python

Python for loop increment by 2

We often use a for loop in python to iterate over a container object like a list or tuple. We also use for loops to perform tasks a fixed number of times. In python, the iterator or value in a for loop increments by one by default. In this article, we will see how we can increment for loop by 2 in Python.

Python For Loop Increment By 2 Using the range() Function

We use the range() function to implement a for loop in python. The range() function is first used to create a sequence and then we execute the for loop using the sequence. The syntax for the range() function is as follows.

Here,

  • start is the number at which the output sequence starts. It is an optional parameter.
  • end is the number at which the output sequence terminates. The output sequence contains numbers only till end-1. It is a mandatory parameter.
  • step  is the common difference between the consecutive numbers in the sequence. It is an optional parameter.

As the parameters start and step are optional. We can also use the range() function as follows.

Here, whenever start is not defined, the default value for start is taken to be 0. If step is not defined, the default value is taken to be 1.

To increment a for loop by 2, we just have to give the value 2 as the step parameter as follows.

Output:

Here, you can observe that we printed a sequence of 10 numbers from 1 to 20 using the range() function. As we have specified the step parameter as 2, the for loop increments by 2 because the range() function returns a sequence having a difference of 2 between the consecutive elements.

Python For Loop Increment By 2 Using List Slicing

In python, we normally iterate through a list directly as follows.

Output:

In this approach, we can access consecutive elements from the list. What if we had to increment the iterator by 2? In such cases, we can use slicing. The syntax for slicing a list is as follows.

newList= myList[startIndex, endIndex,step]

Here,

  • myList is the input list and newList is the list created by slicing myList.
  • startIndex is the index of the element in myList from which we start including the elements in the newList. Here, you can leave the startIndex empty if you want to include elements right from the start.
  • endIndex is the index of the element in myList at which we stop including elements of myList in newList. Here, you can leave the endIndex empty if you want to include elements till last.
  • step denotes the number of elements that we skip in myList before including the next element to newList

To increment the iterator of the for loop by 2 while iterating a list, we can specify the step as 2 using slicing as follows.

Output:

Here, you can observe that we have first printed all the elements of a list. Then, we have created a slice of the original list to increment the for loop by 2 elements.

For iterating a list, I would suggest you not use the slicing approach. This is so because the sliced list also requires space. So, for larger lists, it might increase the use of memory space. Alternatively, you can use the range() function and indexing to access the elements at an interval of 2 from the original list as follows.

Output:

Conclusion

In this article, we have discussed two ways to increment a for loop by 2 in python. We have also seen why we should prefer the approach that uses the range() function instead of the slicing method. 

I hope you had fun reading this blog. Stay tuned for more informative articles. 

Happy Learning.

Was this post helpful?

Leave a Reply

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