Three ways to Reverse a List/Array/Tuple in Python

  • 时间:2020-09-16 12:48:17
  • 分类:网络文摘
  • 阅读:130 次
python Three ways to Reverse a List/Array/Tuple in Python python

python

Reversing a List/Array is very commonly needed and there are three ways to reverse a list or array in Python.

Using the .reverse() method to reverse a list or array

Note: this method does not work for tuples in Python.

1
2
3
4
5
6
a
(1, 2, 3, 4, 5)
>>> a.reverse()
Traceback (most recent call last):
  File "stdin", line 1, in module
AttributeError: 'tuple' object has no attribute 'reverse'
a
(1, 2, 3, 4, 5)
>>> a.reverse()
Traceback (most recent call last):
  File "stdin", line 1, in module
AttributeError: 'tuple' object has no attribute 'reverse'

The .reverse() will modify the given array/list. See the below:

1
2
3
a = [1, 2, 3, 4, 5]
a.reverse() # returns None
print(a) # [5, 4, 3, 2, 1]
a = [1, 2, 3, 4, 5]
a.reverse() # returns None
print(a) # [5, 4, 3, 2, 1]

As we can see in the above Python code, the .reverse() does not create a copy, rather, it reverses the array/list in-place. The .reverse() method returns None.

Using the slicing to reverse an array, list or tuples in Python

We can use the [::-1] to returns a copy, without modifying the input. It works for list/array and tuples as well.

1
2
3
4
5
6
7
8
9
10
11
>>> a = (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
>>> a[::-1]
(5, 4, 3, 2, 1)
>>> a
(1, 2, 3, 4, 5)
>>> a=[1,2,3,4,5]
>>> a[::-1]
[5, 4, 3, 2, 1]
>>> a
[1, 2, 3, 4, 5]
>>> a = (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
>>> a[::-1]
(5, 4, 3, 2, 1)
>>> a
(1, 2, 3, 4, 5)
>>> a=[1,2,3,4,5]
>>> a[::-1]
[5, 4, 3, 2, 1]
>>> a
[1, 2, 3, 4, 5]

This is quite cool and the most Pythonic way to reverse a list or tuple.

Python reversed() method to return a reversed iterator

We can use the reversed() function (inbuilt Python) that allows us to return an iterator. As for the list/array:

1
2
3
4
a = [1, 2, 3, 4, 5]
reversed(a) # returns an iterator
list(reversed(a)) converts to list [5, 4, 3, 2, 1]
tuple(reversed(a)) converts to tuple (5, 4, 3, 2, 1)
a = [1, 2, 3, 4, 5]
reversed(a) # returns an iterator
list(reversed(a)) converts to list [5, 4, 3, 2, 1]
tuple(reversed(a)) converts to tuple (5, 4, 3, 2, 1)

It works for tuples as well:

1
2
3
4
a = (1, 2, 3, 4, 5)
reversed(a) # returns an iterator
list(reversed(a)) converts to list [5, 4, 3, 2, 1]
tuple(reversed(a)) converts to tuple (5, 4, 3, 2, 1)
a = (1, 2, 3, 4, 5)
reversed(a) # returns an iterator
list(reversed(a)) converts to list [5, 4, 3, 2, 1]
tuple(reversed(a)) converts to tuple (5, 4, 3, 2, 1)

Returning a reversed() iterator does not modify the original array, list or tuple as well. When you call list() or tuple(), you can convert the reversed iterator explicitly to a list or tuple.

Oh, it seems years ago I have written on this topics already: Reverse List/Tuple/String in Python

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
吃水果的禁忌以及富含维生素的水果  黄瓜的营养价值保健效果及食用方法  土鸡蛋与洋鸡蛋的营养价值以及区别  冬季适当吃糯米类食物有御寒滋补之功效  三类护耳食物可以延缓老年人听力下降  红枣养生知识:食用红枣需注意的问题  健康面点拒绝这些有毒的添加剂原料  火锅健康吃法:先素后荤 煮烫适度 少吃辣  柚子的保健功效以及食用柚子的禁忌  许多人已经走入了补充益生菌的误区 
评论列表
添加评论