Python 中的列表和元組是 Python 數據結構的兩類。列表結構是動態的,很容易改變,而元組結構是靜態的,不能改變。這意味著元組通常比列表更快。列表用方括號表示,元組用括號表示。
Python 中列表和元組的區別
斯諾  | LIST  | TUPLE  | 
|---|---|---|
| 1 | 列表是可變的 | 元組是不可變的 | 
| 2 | 迭代的含義是耗時的 | 迭代的含義是相對更快 | 
| 3 | 該列表更適合執行插入和刪除等操作。 | 元組數據類型適合訪問元素 | 
| 4 | 列表消耗更多內存 | 與列表相比,元組消耗的內存更少 | 
| 5 | 列表有幾個內置方法 | Tuple 沒有很多內置方法。 | 
| 6 | 更容易發生意外的變化和錯誤 | 因為元組不會改變,所以它們遠不如error-prone。 | 
Python 列表與 Python 元組
測試元組是否不可變而列表是否可變
在這裏,我們將比較列表和元組的可變性測試。
Python3
# Creating a List with
# the use of Numbers
# code to test that tuples are mutable
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("Original list ", List)
List[3] = 77
print("Example to show mutability ", List)
相關用法
- Python List和Dictionary的區別用法及代碼示例
 - Python List和Array的區別用法及代碼示例
 - Python List append()用法及代碼示例
 - Python List extend()用法及代碼示例
 - Python List insert()用法及代碼示例
 - Python List remove()用法及代碼示例
 - Python List index()用法及代碼示例
 - Python List count()用法及代碼示例
 - Python List pop()用法及代碼示例
 - Python List reverse()用法及代碼示例
 - Python List sort()用法及代碼示例
 - Python List copy()用法及代碼示例
 - Python List clear()用法及代碼示例
 - Python List cmp()用法及代碼示例
 - Python List len()用法及代碼示例
 - Python List max()用法及代碼示例
 - Python List min()用法及代碼示例
 - Python List list()用法及代碼示例
 - Python List remove方法用法及代碼示例
 - Python List insert方法用法及代碼示例
 - Python List copy方法用法及代碼示例
 - Python List append方法用法及代碼示例
 - Python List clear方法用法及代碼示例
 - Python List pop方法用法及代碼示例
 - Python List sort方法用法及代碼示例
 
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Difference Between List and Tuple in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
