Reverse a List, String, Tuple in Python: reverse, reversed

Modified: | Tags: Python, List, String

In Python, you can reverse a list using the reverse() method, the built-in reversed() function, or slicing. To reverse a string (str) and a tuple, use reversed() or slicing.

If you want to sort in ascending or descending order instead of in reverse order, see the following article.

Reverse a list using the reverse() method

reverse() is a method for mutable sequence types such as lists.

reverse() is a destructive process that reverses the original list in place.

l = [1, 2, 3, 4, 5]

l.reverse()
print(l)
# [5, 4, 3, 2, 1]

Note that reverse() returns None.

print(l.reverse())
# None

Examples of mutable sequence types other than lists include types like collections.deque. The reverse() method is also available for collections.deque.

Reverse a list using the built-in reversed() function

reversed() is a built-in function.

The built-in reversed() function generates an iterator that retrieves elements in reverse order, leaving the original list unchanged.

l = [1, 2, 3, 4, 5]

print(type(reversed(l)))
# <class 'list_reverseiterator'>

print(l)
# [1, 2, 3, 4, 5]

Note that reversed() returns an iterator, not a list.

You can directly use it in a for loop.

for i in reversed(l):
    print(i)
# 5
# 4
# 3
# 2
# 1

If you want to get a list in reverse order, use list() to convert the iterator to a list.

l_reversed = list(reversed(l))
print(l_reversed)
# [5, 4, 3, 2, 1]

print(l)
# [1, 2, 3, 4, 5]

Reverse a list using slicing

You can also use slicing to reverse a list.

In slicing, specify a range or increment using the format [start:stop:step].

If start and stop are omitted, the whole list is selected, and by setting step to -1, items can be retrieved one by one in reverse order.

You can get the reverse list using the slicing notation [::-1].

l = [1, 2, 3, 4, 5]

l_reversed = l[::-1]
print(l_reversed)
# [5, 4, 3, 2, 1]

print(l)
# [1, 2, 3, 4, 5]

See the following article for details of slicing.

Reverse a string

Since strings are immutable, they do not have a reverse() method that updates the original object. To reverse a string, you can use reversed() or slicing.

You cannot convert an iterator to a string directly. When using reversed(), you'll first need to convert the iterator to a list containing single characters, and then join them using the join() method.

s = 'abcde'

s_reversed_list = list(reversed(s))
print(s_reversed_list)
# ['e', 'd', 'c', 'b', 'a']

s_reversed = ''.join(list(reversed(s)))
print(s_reversed)
# edcba

The method of reversing using slicing is the same for both strings and lists. This slicing method is often considered easier than using reversed().

s_reversed = s[::-1]
print(s_reversed)
# edcba

Reverse a tuple

Reversing tuples follows the same approach as with strings, since both are immutable and do not have a reverse() method.

Use tuple() to convert an iterator to a tuple.

t = (1, 2, 3, 4, 5)

t_reversed = tuple(reversed(t))
print(t_reversed)
# (5, 4, 3, 2, 1)

The method using slicing is the same as for lists.

t_reversed = t[::-1]
print(t_reversed)
# (5, 4, 3, 2, 1)

Related Categories

Related Articles