程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The difference between Python list and tuple

編輯:Python

stay Python in , list (list) And a tuple (tuple) It is a kind of data structure that can store one or more objects or values . Lists are used to store multiple items in a variable , And you can use square brackets to create . Again , Tuples can also store multiple items in a single variable , And you can use parentheses to declare .

although list and tuple There are many differences , But there are some similarities , as follows :

Both of these data structures are sequential data types that store collections of items .
Items of any data type can be stored in it .
Items can be accessed through their indexes .

Catalog

Lists and tuples

Grammatical differences

variability

function

Size

Element type

length

debugging

Nested lists and tuples

purpose

Conclusion


Lists and tuples

list Tuples It's variable It is immutable Iterations are time-consuming in the list . Iterations in tuples are much faster . Operations such as insert and delete perform better . Better access to elements . Consume more memory . Consume less memory . Many built-in methods are available . There are not many built-in methods . Unexpected errors can easily appear in the list . Unexpected errors rarely occur in tuples .

 

 

Grammatical differences


As mentioned in the introduction ,list and tuple The grammar of is different . for example :

list_num = [10, 20, 30, 40]

tup_num = (10, 20, 30, 40)

Besides , Please check out our business analysis course to broaden your horizons .

variability


One of the most important differences between lists and tuples is that lists are mutable , And tuples are immutable . This means that the list can be changed , You cannot change tuples .

therefore , Some operations can work on the list , But it can't work on tuples . for example , In Data Science , If the list already exists , You can reallocate specific elements . besides , You can reassign the entire list . You can delete elements and element slices from the list .

On the other hand , Specific elements on tuples cannot be reassigned or deleted , But it can be sliced , Even reassign and delete the entire tuple . Because tuples are immutable , So they cannot be copied .


Although there are many operations like lists and tuples , But lists have additional functions that tuples do not . These are insert and eject operations , And sorting and deleting elements in the list .

function


some Python Function can be applied to these two data structures , for example len、max、min、any、sum、all and sorted.

Size


stay Python in , Tuples are allocated large chunks of memory with low overhead , Because they are immutable ; And for the list , Allocated is a small memory block . In between , Tuples have less memory . When there are a lot of elements , This helps make tuples faster than lists .

Element type


Elements belonging to different data types , Heterogeneous elements , Usually stored in tuples . And homogeneous elements , That is, elements of the same data type , Usually stored in a list . But this is not a limitation on the data structure . Similar data type elements can be stored in tuples , Different data type elements can also be stored in the list .

length


The length of the two data structures is different . Tuples have a fixed length , The list has variable length . therefore , You can change the size of the created list , But tuples are not like this .

debugging


In terms of commissioning , In lists and tuples , Tuples are easier to debug for large projects because of their invariance . therefore , If the project is relatively small or the amount of data is relatively small , It's better to use a list . This is because the list can be changed , Tuples cannot , This makes tuples easier to track .

Nested lists and tuples


Tuples can be stored in lists , Again , Lists can also be stored in tuples . In nested tuples , One tuple can hold more tuples . In nested list , One list can hold more lists .

purpose


It is important to understand that it is best to use one of these data structures in different situations , for example ; Which one to use depends on the programmer , That is, choose one according to whether they want to change the data in the future .

Tuples can be equivalent to a dictionary without keys to store data . When tuples are stored in a list , Easier to read data .

Conclusion


This article helps to understand the difference between lists and tuples . Although both types are Python Data structure in , But it is important to be familiar with these differences when making choices . The most important difference to remember is The list is variable , Tuples are not , Lists have variable sizes and tuples have fixed sizes . Last , Operations in tuples can be performed faster .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved