We can start the for a loop at index 1 in several ways in Python, in general, 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 sequence objects used in for loop start from the ‘0’ index, but, sometimes you would be required to start the index at ‘1’.
You can do this by using various functions such as range(), len(), enumerate(), nested loops, and slicing techniques. In this article, I will explain how to start for loop at index ‘1’ in different ways with examples.
1. Quick Examples of For Loop Starting at Index 1
If you are in a hurry, below are some quick examples of how to start for loop starting at index 1.
# Quick examples of for loop starting at index 1
# Example 1: To start the loop at '1"
# Using range()
n = 6
for x in range(1, n+1):
# Example 2: Use range() & len()
# To start the loop at '1'
list=[10, 20, 50, 30, 40, 80, 60]
for i in range(1, len(list)):
# Example 3: Start for loop index at '1'
# Using enumerate()
list=[10, 20, 50, 30, 40, 80, 60]
for i, value in enumerate(list, 1):
# Example 4: Use nested for loops
# To start the loop at '1'
for i in (n+1 for n in range(6)):
# Example 5: Use nested for loops
# To start the loop at '1'
for i in (n+1 for n in range(6)):
# Example 6: Using slicing to start
# The loop at '1'
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[1:]:
2. For Loop Start Index at 1 in Python
To access sequence (list, set, tuple etc) values by index in for loop we typically use the range() that returns the first value as 0 by default, when you want to start the for loop with index at 1, use the range() with start param as 1.
Let’s quickly see how we can use the range() to get the sequence values from starting 1 and then use this on sequence objects.
# Start the loop at index '1"
# Using range()
for x in range(1, 7):
print(x)
Yields below output.
3. Range with For Loop to Start at 1 in Python
To start the for loop with index at 1 in Python use the range() with start param at 1 and for the end value use the len() which gives the length of the sequence object. With this we can start the for loop at index 1. Note that when you used with index 1, you going to skip the value at index 0.
# Use range() & len() to start the loop at '1'
list=[10, 20, 50, 30, 40, 80, 60]
for i in range(1, len(list)):
print(i, list[i])
Yields below output.
4. Start For Loop at ‘1’ Using enumerate() Function
The enumerate() function is one of the most efficient when we want to check the index in for loop iterating over a sequence in Python. By default, enumerate() function returns from ‘0’ indexes with corresponding values of the given iterable object. Here, I will pass the specified starting index along with the list into enumerate() function and then iterate it using for loop. It returns values with their corresponding indexes where indexes start from ‘1’.
# Start for loop index at '1' using enumerate()
list=[10, 20, 50, 30, 40, 80, 60]
for i, value in enumerate(list, 1):
print(i, value)
# Output:
# 1 10
# 2 20
# 3 50
# 4 30
# 5 40
# 6 80
# 7 60
5. Using Nested Loops Start at ‘1’
Use nested for loops to start the loop at ‘1’ in Python. The loop inside another loop is called a nested loop.
In the below example, the generator expression (n+1 for n in range(6)) creates a sequence of numbers starting from 1 up to 6. The for loop then iterates over this sequence, and the variable i takes on each value in the sequence.
# Use nested for loops to start the loop at '1'
for i in (n+1 for n in range(6)):
print(i)
# Output:
# 1
# 2
# 3
# 4
# 5
# 6
6. Using Slicing Technique to Start the Loop at ‘1’
You can use the Python slicing technique [start:] you can start the loop at the ‘1’ index, it will skip the first index ‘0’ and start the loop at index ‘1’.
If you want to use slicing to start a loop at index ‘1’, you can slice the iterable (e.g., a list) to exclude the element at index 0. For example, list[1:] slices the list starting from index 1 to the end of the list, effectively excluding the element at index 0.
# Using slicing to start the loop at '1'
list=[10, 20, 50, 30, 40, 80, 60]
for i in list[1:]:
print(i)
# Output:
# 20
# 50
# 30
# 40
# 80
# 60
Frequently Asked Questions
To start a Python for loop at 1 instead of 0, you can use the range function and specify a starting value of 1. For example, range(1,6) generates a sequence of numbers starting from 1 and going up to, but not including, 6. The for loop then iterates over these numbers, and the variable i takes on each value in the sequence.
There are alternative methods to start a for loop at 1 in Python. One alternative is to use a generator expression. For instance, the generator expression (n+1 for n in range(8)) creates a sequence of numbers starting from 1 up to 8. The for loop then iterates over this sequence, and the variable i takes on each value in the sequence.
It is possible to customize the starting point and step of a for loop in Python using the range function. The range function allows you to specify the start, end, and step values for the sequence of numbers generated.
Python follows the convention of zero-based indexing, which aligns with the language’s design principles. However, the range function allows flexibility in specifying the starting point.
If you want to nest loops and start the outer loop at 1, you can use the range function for each loop, adjusting the range accordingly.
Conclusion
In this article, I have explained how to start for loop at index ‘1’ in Python using the range(), len(), enumerate(), nested loops, and slicing techniques with examples.
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?
- Use For Loop to Iterate Tuple in Python
- Access Index in for Loop in Python
- Skip Iterations in a Python For Loop
- Ways to Loop Through a List in Python
- How to Write Python For Loop in One Line?
- How to terminate a script in Python?