How to decrement (for example by 2) for loop in Python? usually, Python for loop is incremented by 1 value however sometimes you would be required to decrement by 1, 2, 3, 4 e.t.c. You can easily achieve this by using the range() function, with the range you can decrement the for loop index with a negative step value.
The for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. By default the for loop increments by ‘1’ for every iteration. If we want to decrement the counter value with the specified number we can go with the range() function.
1. Quick Examples of Using Decrement in For Loop
If you are in a hurry, below are some quick examples of decrementing a counter with for loop.
# Quick examples of using decrement in for loop
# Example 1: Customized decrement in a for loop
# Using range() function
for i in range(6, 0, -1):
# Example 2: Decrement for loop by 2
for x in range(6, 0, -2):
# Example 3: Decrement for loop by 2
# Using len() function
list=[10, 20, 50, 30, 40, 80, 60]
for x in range(len(list)-1, -1, -2):
print(list[x])
# Example 4: Initialize the decrement value with integer
x = -3
for i in range(10, 0, x):
# Example 5: Decrement for loop
# Using reversed()
for i in reversed(range(1,6)):
# Example 6: Decrement the value
# Using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[-1::-3]:
# Example 7: Decrement for loop
# Using list comprehension
y = 1
for i in ([x*y for x in range(50, 10, -10)]):
# Example 8: Decrement the float values in a for loop
import numpy as np
for i in np.arange(20.5, 10.5, -2.5):
2. Syntax of range()
Following is the syntax of the range() function. From the params, we use a step param with a customs value in for loop to decrement.
# Syntax of range() function
range(start, stop, step)
Following are the parameters of the range() function. It returns a list of series.
start(optional) – The starting value of the sequence. If omitted, it defaults to 0.stop– The end value of the sequence (exclusive). The sequence will go up to, but not include, this value.step(optional) – The step or increment between values in the sequence. If omitted, it defaults to 1.
3. Using range() Decrement for Loop
Python range() function is used to generate a sequence of numbers within a given range, to decrement the loop we can customize the step value with a specified negative value. If you want to use range() with decrement for a loop in Python, you can set the step parameter to a negative value. For example,
# Customized decrement in a for loop
# Using range()
for i in range(6, 0, -1):
print(i)
Yields below output.
As we can see from the above the numbers we have got 6, 5, 4, 3, 2, and 1 in decrement order. Here, the start argument of the range() function starts from the value 6, the stop argument ends with 0 and the third argument step is set with -1.
4. Python For Loop Decrement by 2
When you want to decrement for loop by 2 we have to specify the step value of the range() function with -2. It will decrement the loop with the specified step value.
In the below example, start is set to 6. stop is set to 0 (exclusive). step is set to -2, indicating that the loop variable x will be decremented by 2 in each iteration.
# Decrement for loop by 2
for x in range(6, 0, -2):
print(x)
Yields below output.
This loop will print values starting from 6 and decrementing by 2 in each step until it reaches or goes below 0.
5. Python For Loop Decrement by 2 using len()
If you want to use the len() function to control the number of iterations in a for loop while decrementing the loop variable by 2, you can achieve that by creating a range based on the length of a sequence.
In the below example, len(list) - 1 is used as the starting index to ensure it points to the last element of the list. stop is set to -1 to include the first element of the list. step is set to -2, indicating that the loop variable x will be decremented by 2 in each iteration.
# Decrement for loop by 2
# Using len()
list=[10, 20, 50, 30, 40, 80, 60]
for x in range(len(list)-1, -1, -2):
print(list[x])
# Output:
# 60
# 40
# 50
# 10
6. Decrement For Loop using Variable
Use the specified integer variable assigned with the decrement value in a for loop using the range() function. Passing the negative integer value into range() as a step parameter it will decrement the for loop with a specified negative integer.
# Initialize the decrement value with integer
x = -3
for i in range(10, 0, x):
print(i)
# Output:
# 10
# 7
# 4
# 1
7. Decrement a for Loop in Python Using the reversed()
Alternatively, we can use reversed() function to decrement the for loop. Pass range() function into reversed() function(will reverse the range() function without any modification) and it using for loop. This syntax will return the values in decrement order.
# Decrement for loop
# Using reversed()
for i in reversed(range(1,6)):
print(i)
# Output:
# 5
# 4
# 3
# 2
# 1
8. Decrement For loop using Slicing
Python slicing is a useful technique when we want to get a specified step of decrement in for loop. Let’s apply the slicing technique with for loop by decrementing 3. The slicing list[-1::-3] starts from the last element (-1), goes all the way to the beginning (None), and steps backward by 3.
# Decrement the value using slicing
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[-1::-3]:
print(i)
# Output:
# 60
# 30
# 10
9. Increment For loop using List Comprehension
Using list comprehension we can decrement inside the loop. Here, I will decrement the values in a for loop using list comprehension.
In the below example, range(50, 10, -10) generates values from 50 down to 20 with a step of -10. The list comprehension [x * y for x in range(50, 10, -10)] creates a list by multiplying each value by y.
# Decrement for loop
# Using list comprehension
y = 1
for i in ([x*y for x in range(50, 10, -10)]):
print(i)
# Output:
# 50
# 40
# 30
# 20
10. Increment For loop using Float values
Finally, we can also decrement for loop with float values. For float values, we have to use numpy.arange() functions with start, stop and step value. Before going to use np.arange() we have to import the numpy module as an np.
# Decrement the float values in a for loop
import numpy as np
for i in np.arange(20.5, 10.5, -2.5):
print (i)
# Output:
# 20.5
# 18.0
# 15.5
# 13.0
Frequently Asked Questions on Perform For Loop Decrement in Python
To iterate over a range of values in reverse order (decrementing), you can use the range function with the start, stop, and step arguments. This loop starts from 10, decrements by 1 in each iteration, and stops when it reaches 1 (the second argument of range is exclusive). Adjust the parameters according to your specific requirements.
You can use a variable as the decrement value in a for loop in Python. The range() function allows you to specify the step value, which can be a variable.
To iterate over a list in reverse order in Python, you can use the reversed() function or use negative indexing.
Another way to iterate over a list in reverse order without using range or reversed() is to directly use negative indexing in a simple for loop. This loop starts from the last element (my_list[-1]) and iterates towards the first element using negative indices.
If you want to decrement the loop variable by a different value in each iteration, you can manually update the loop variable within the loop body. For example, decrement_values is a list containing different decrement values for each iteration. Inside the loop, the loop variable (i in this case) is decremented by the current value in the decrement_values list.
There is an alternative to using the range() function for a decrementing for loop in Python. You can use a while loop with a condition that determines when to stop the iteration.
Conclusion
In this article, I have explained the Python range() function and how we can decrement the for loop by custom values like 2, 3, etc. To perform the decrement you have to use the step param with the value you wanted to decrement.
Related Articles
- For Loop with If Statement in Python
- For Loop Break Statement in Python
- For Loop Iterate Over an Array in Python
- For Loop Continue And Break in Python
- How to Increment for Loop in Python
- For Loop Enumerate in Python
- Get Counter Values in a for Loop in Python?
- Access Index in for Loop in Python
- How to Start Python For Loop at 1
- Skip Iterations in a Python For Loop