Python中的 List 和 Tuple 是数据结构的类。列表是动态的,而元组具有静态特性。
List就像数组一样,在其他语言中声明。列表不必总是同质的,这使其成为Python最强大的工具。在Python,列表是数据结构中的一种容器,用于同时存储多个数据。列表是保存数据序列并进一步迭代它的有用工具。
句法:
list_data = ['an', 'example', 'of', 'a', 'list']
元组也是一种序列数据类型,可以包含不同数据类型的元素,但这些元素本质上是不可变的。换句话说,元组是由逗号分隔的Python对象的集合。由于本质上是静态的,元组比列表快。
句法:
tuple_data = ('this', 'is', 'an', 'example', 'of', 'tuple')
Python列表和元组的区别:
SR.NO. | LIST | TUPLE |
---|---|---|
1 | Lists are mutable | Tuples are immutable |
2 | Implication of iterations is Time-consuming | The implication of iterations is comparatively Faster |
3 | The list is better for performing operations, such as insertion and deletion. | Tuple data type is appropriate for accessing the elements |
4 | Lists consume more memory | Tuple consume less memory as compared to the list |
5 | Lists have several built-in methods | Tuple does not have many built-in methods. |
6 | The unexpected changes and errors are more likely to occur | In tuple, it is hard to take place. |