Python列表reverse()方法

Python列表reverse()方法用於將列表中的對象反轉位置。

語法

以下是reverse()方法的語法 -

list.reverse()

參數

  • NA

返回值

  • 此方法不返回任何值,但從列表中反轉給定對象。

示例

以下示例顯示了reverse()方法的用法。

#!/usr/bin/python3
list1 = ['physics', 'Biology', 'chemistry', 'maths']
list1.reverse()
print ("list now : ", list1)

當運行上面的程式,它產生以下結果 -

list now :  ['maths', 'chemistry', 'Biology', 'physics']

從上面輸出結果中可以看到,列表中的每個元素都顛倒了。


上一篇: Python列表 下一篇: Python元組