Home > Backend Development > Python Tutorial > How to iterate a sequence in reverse order in Python?

How to iterate a sequence in reverse order in Python?

WBOY
Release: 2023-08-25 20:57:10
forward
721 people have browsed it

How to iterate a sequence in reverse order in Python?

Python sequences include strings, lists, tuples, etc. We can combine elements of Python sequences in different ways. Let's look at some examples of iterating a list in reverse order.

Use a while loop to iterate in reverse order

Example

In this example we have a list as a sequence and use a while loop to iterate in reverse order -

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Length - 1
i = len(mylist) - 1

# Iterate in reverse order
print("Display the List in Reverse order = ")
while i >= 0 :
   print(mylist[i])
   i -= 1
Copy after login

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob
Copy after login
Copy after login
Copy after login
Copy after login

Use a for loop to iterate in reverse order

Example

In this example we have a list as a sequence and use a for loop to iterate in reverse order -

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Iterate in reverse order
print("Display the List in Reverse order = ")
for i in range(len(mylist) - 1, -1, -1):
   print(mylist[i])
Copy after login

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob
Copy after login
Copy after login
Copy after login
Copy after login

Use reverse() to iterate in reverse order

Example

In this example, we have a List as a sequence and use the reversed() method to iterate in reverse order -

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Iterate in reverse order using reversed()
print("Display the List in Reverse order = ")
[print (i) for i in reversed(mylist)]
Copy after login

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob
Copy after login
Copy after login
Copy after login
Copy after login

Use negative indexes for reverse iteration

Example

In this example, we take the List as a sequence and use negative indexes to iterate over it in reverse order

# Creating a List
mylist = ["Jacob", "Harry", "Mark", "Anthony"]

# Displaying the List
print("List = ",mylist)

# Iterate in reverse order using negative indexing
print("Display the List in Reverse order = ")
[print (i) for i in mylist[::-1]]
Copy after login

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Display the List in Reverse order = 
Anthony
Mark
Harry
Jacob
Copy after login
Copy after login
Copy after login
Copy after login

The above is the detailed content of How to iterate a sequence in reverse order in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template