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


Written By - Azka Iftikhar
Advertisement

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 :

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

 

Further Reading

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

 

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.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment