Python iterate through list backwards – Python : Different ways to Iterate over a List in Reverse Order

Python iterate through list backwards: Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they’ve been created.

We’ll look at a few different ways to iterate over a List in Reverse Order in this article.

Example:

Input:

givenlist=['hello' , 'world' , 'this', 'is' ,'python']

Output:

python
is 
this
world
hello

Print list in reverse order

Python iterate list backwards: There are several ways to iterate through a list in reverse order some of them are:

Method #1 : Using for loop and range()

Iterate through list backwards python: When it comes to iterating through something, the loop is always helpful. To iterate in Python, we use the range() function. This approach is known as range ([start], stop[, step]).

start: This is the sequence’s first index.
stop: The range will continue until it reaches this index, but it will not include it.
step: The difference between each sequence element.

range(len(givenlist)-1, -1, -1)

this will return numbers from n to 1 i.e reverse indexes.

In the for loop, use the range() function and the random access operator [] to access elements in reverse.

Below is the implementation:

# Given list
givenlist = ['hello', 'world', 'this', 'is', 'python']

# Iterate over the list in reverse order using for loop and range()

for index in range(len(givenlist) - 1, -1, -1):
    print(givenlist[index])

Output:

python
is 
this
world
hello

Method #2: Using while loop

In Python, the while loop is used to iterate through a block of code as long as the test expression (condition) is true. This loop is typically used when we don’t know how many times to iterate ahead of time.

  • First, we used the len() method to determine the length of the list.
  • The index variable is set to the length of the list -1.
  • It’s used to display the list’s current index when iterating.This loop will continue until the index value reaches 0.
  • The index value is decremented by one each time.
  • The print line will print the list’s current iteration value.

Below is the implementation:

# Given list
givenlist = ['hello', 'world', 'this', 'is', 'python']

# Point position to the last element in list
position = len(givenlist) - 1

# Iterate till 1st element and keep on decrementing position
while position >= 0:
    print(givenlist[position])
    position = position - 1

Output:

python
is 
this
world
hello

Method #3: Using list Comprehension and slicing

givenlist[::-1]

It will generate a temporary reversed list.

Let’s apply this to iterating over the list in reverse in List comprehension.

Below is the implementation:

# Given list
givenlist = ['hello', 'world', 'this', 'is', 'python']

# Using list comprehension and slicing
[print(element) for element in givenlist[::-1]]

Output:

python
is 
this
world
hello

Method #4: Using for loop and reversed()

reversed(givenlist)

The reversed() function returns an iterator that iterates through the given list in reverse order.

Let’s use for loop  to iterate over that reversed sequence.

Below is the implementation:

# Given list
givenlist = ['hello', 'world', 'this', 'is', 'python']

# Using for loop and reversed function
for element in reversed(givenlist):
    print(element)

Output:

python
is 
this
world
hello

Method #5 : Using List Comprehension and reversed() function

reversed(givenlist)

The reversed() function returns an iterator that iterates through the given list in reverse order.

let us use list comprehension to print list in reverse order.

Below is the implementation:

# Given list
givenlist = ['hello', 'world', 'this', 'is', 'python']

# Iterate over the list using List Comprehension and [::-1]
[print(element) for element in reversed(givenlist)]

Output:

python
is 
this
world
hello

Related Programs: