note.nkmk.me

Reverse a list, string, tuple in Python (reverse, reversed)

Posted: 2020-01-30 / Tags: Python, List, String

In Python, you can reverse the items of lists (list) with using reverse(), reversed(), and slicing. If you want to reverse strings (str) and tuples (tuple), use reversed() or slice.

Here, the following contents will be described.

  • List type method reverse() reverses the original list
  • Built-in function reversed() returns a reverse iterator
  • Reverse with slicing
  • Reverse strings and tuples

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

Sponsored Link

List type method reverse() reverses the original list

reverse() is a list type method.

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

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

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

Note that reverse() returns None.

print(org_list.reverse())
# None

Built-in function reversed() returns a reverse iterator

reversed() is a built-in function.

Built-in function reversed() generates an iterator that retrieves elements in reverse order

reversed () returns an iterator that retrieves the items in reverse order. The original list remains unchanged.

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

reverse_iterator = reversed(org_list)
print(org_list)
# [1, 2, 3, 4, 5]

print(type(reverse_iterator))
# <class 'list_reverseiterator'>

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

You can just use it in a for loop.

for i in reversed(org_list):
    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.

new_list = list(reversed(org_list))
print(org_list)
print(new_list)
# [1, 2, 3, 4, 5]
# [5, 4, 3, 2, 1]
Sponsored Link

Reverse with slicing

You can also use a slicing to reverse lists.

In slicing, specify a range or increment in the form [start:stop:end].

If start and stop are omitted, the whole list is selected, and by setting step to -1, items can be obtained one by one from the back.

You can get the reverse list by [::-1].

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

new_list = org_list[::-1]
print(org_list)
print(new_list)
# [1, 2, 3, 4, 5]
# [5, 4, 3, 2, 1]

See the following post for details of slicing.

Reverse strings and tuples

Since strings and tuples are immutable, there is no reverse() method that update the original object.

If you want to reverse strings or tuples, use reversed() or slices.

Reverse strings

Since iterators cannot be converted directly to strings, when using reversed(), convert the iterator to a list containing one character and then join them with join().

org_str = 'abcde'

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

new_str = ''.join(list(reversed(org_str)))
print(new_str)
# edcba

The method using slicing is the same as for lists. This is easier than using reversed().

new_str = org_str[::-1]
print(new_str)
# edcba

Reverse tuples

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

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

new_tuple = tuple(reversed(org_tuple))
print(new_tuple)
# (5, 4, 3, 2, 1)

The method using slicing is the same as for lists.

new_tuple = org_tuple[::-1]
print(new_tuple)
# (5, 4, 3, 2, 1)
Sponsored Link
Share

Related Categories

Related Posts