Name

reversed()

Examples
numbers = [1, 2, 3]
reversed_numbers = reversed(numbers)
for num in reversed_numbers:
  print(num)
# Prints:
# 3
# 2
# 1

# Meanwhile, the original list has not been modified:
print(numbers) # Prints [1, 2, 3]
# The reversed() function returns an iterator by default. Use list() to
# convert the iterator to a list.
numbers = [1, 2, 3]
reversed_numbers = reversed(numbers)
reversed_list = list(reversed_numbers)
print(reversed_list) # Prints [3, 2, 1]
# reversed() can reverse any iterable, not just lists
word = "orderly"
reversed_word = reversed(word)
reversed_list = list(reversed_word)
print(reversed_list) # Prints ['y', 'l', 'r', 'e', 'd', 'r', 'o']
Description Return an iterator that will produce the elements of the given list (or other iterable) reverse order. This function differs from the list object's reverse() method in that it does not modify the list in-place. It can also take as a parameter any iterable (e.g., a string, dictionary, or tuple), not just a list (as shown in the string example above).

Because reversed() returns an iterator, you normally need to iterate over its result in a for loop (as shown in the first example above). You can use the list() function to get the entire reversed iterable in one go.
Syntax
	reversed(x)
Parameters
xThe iterable (list, string, dictionary, etc.) to reverse
Related .reverse()
.sort()
list()
sorted()

Updated on Tue Feb 27 14:07:12 2024.

If you see any errors or have comments, please let us know.