How to Start Python For Loop at 1

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

Following are quick examples of how to start for loop starting at index 1.


# Below are the quick examples

# 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 form 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)
Python For Loop Start at 1

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])
Python For Loop Start at 1

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 nested loop. For example,


# 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:] we can start the loop at the ‘1’ index, it will skip the first index ‘0’ and start the loop at index ‘1’. For example,


# 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

7. 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

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing How to Start Python For Loop at 1