# Print a list in reverse order with range()? [SOLVED]

In this article we will learn how we can print a list in reverse order with `range()` in Python for which first we need to know what range() is and how it works.

## Using the range() function

Range function is a python in-built function that returns a sequence of numbers from 0 to the range.

The function has three parameters range(start, stop, increment)

• Start is starting number
• Stop is the ending number
• Increment is the number of steps it increments.

Lets look at an example of how it works

``````x = range(1, 10)
for n in x:
Â Â print("range element : ",n)``````

The output of this code is :

```range element :Â  1
range element :Â  2
range element :Â  3
range element :Â  4
range element :Â  5
range element :Â  6
range element :Â  7
range element :Â  8
range element :Â  9```

In case you want to increment it by 3

``````# Print a list in reverse order with range()
x = range(1, 20,3)
for n in x:
Â Â print("range element with increment of 3: ",n)``````

The output is :

```range element with increment of 3:Â  1
range element with increment of 3:Â  4
range element with increment of 3:Â  7
range element with increment of 3:Â  10
range element with increment of 3:Â  13
range element with increment of 3:Â  16
range element with increment of 3:Â  19```

## Print a list in reverse order with range()

### Method-1:

Here we will learn how to print a list in reverse order with `range()`. We specify the range and give it the end of the list as the beginning parameter, the ending one becomes 1st and decrement it by one in the loop. The code is as follows :

``````# Print a list in reverse order with range()
a = [1,2,3,4,5,6,7,8,9,10]
print("the list : ",a)
for i in range(len(a)-1, -1, -1):
Â Â Â Â print("list elements after reversing : ",a[i])``````

The output of this code is :

```the list :Â  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list elements after reversing :Â  10
list elements after reversing :Â  9
list elements after reversing :Â  8
list elements after reversing :Â  7
list elements after reversing :Â  6
list elements after reversing :Â  5
list elements after reversing :Â  4
list elements after reversing :Â  3
list elements after reversing :Â  2
list elements after reversing :Â  1```

### Method-2:

Another way is to use the reversed function like this

``````# Print a list in reverse order with range()
a = [1,2,3,4,5,6,7,8,9,10]
print("the list : ",a)
for i in list(reversed(range(10))):
Â Â Â Â print("list elements after reversing : ",a[i])``````

The output of this code is :

```the list :Â  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list elements after reversing :Â  10
list elements after reversing :Â  9
list elements after reversing :Â  8
list elements after reversing :Â  7
list elements after reversing :Â  6
list elements after reversing :Â  5
list elements after reversing :Â  4
list elements after reversing :Â  3
list elements after reversing :Â  2
list elements after reversing :Â  1```

### Method-3:

Another way is using range `(10)[::-1]`

``````# Print a list in reverse order with range()
a = [1,2,3,4,5,6,7,8,9,10]
print("the list : ",a)
for i in range(10)[::-1]:
Â Â Â Â print("list elements after reversing : ",a[i])``````

The output becomes :

```the list :Â  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list elements after reversing :Â  10
list elements after reversing :Â  9
list elements after reversing :Â  8
list elements after reversing :Â  7
list elements after reversing :Â  6
list elements after reversing :Â  5
list elements after reversing :Â  4
list elements after reversing :Â  3
list elements after reversing :Â  2
list elements after reversing :Â  1```

## Conclusion

In this article we studied about the in built function range in python and how we use it to print a list in reverse order with range(). We studied 3 different methods of reversing a list using range. The reversed method, using decrement of -1 while giving the start point to last element. and using an iterator.

Print a list in reverse order with range()? - Stack Overflow

Views: 0

Azka Iftikhar

She is proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can read more on her GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com