In this post we will see some of the difference between list and tuple in Python.Knowing their difference might come in handy when deciding which data type should be preferred over which and when.
The first difference between list and tuple is the format of assigning elements to their object.List uses the square bracket(‘’) while tuple uses the normal bracket (‘()’).
The built-in function type() is use to check the type of the object.
>>> #Tuple type >>> tp=(23 , 56 , 'Text') >>> type(tp) #checking type <class 'tuple'> >>> #List type >>> ls=[23, 56 , 89 , 90] >>> type(ls) #checking type <class 'list'>
To create a single element tuple you need to include coma after the element.If the coma is not written it is interpreted as the type of the element and not as tuple type.But in case of list including coma is not necessary.
>>> tp1=( 'New' ) >>> type( tp1 ) <class 'str'> >>> tp2=( 234 ) >>> type(tp2) <class 'int'> >>> tp3=( 234 , ) >>> type( tp3 ) <class 'tuple'> >>> tp4=() >>> type(tp4) <class 'tuple'> >>> #In case of list >>> ls1= >>> type(ls1) <class 'list'> >>> ls2= >>> type(ls2) <class 'list'>
Look at the above code among tp1,tp2 and tp3 only ‘tp3’ belongs to tuple.
ii)Mutable and immutable
Tuples are immutable.Once tuples are created we cannot add or delete any element from the object.The object will have the same number of elements throughout it’s life time. And also we cannot change the value of the element.
Lists on the other hand are mutable.The elements can be added or deleted from the object.We can also assign new values to the exiting element.
>>> #Tuple example >>> tple=(23 , 45 , 67 , 89) >>> tple=1000 #we cannot change value Traceback (most recent call last): File "<pyshell#81>", line 1, in <module> tple=1000 TypeError: 'tuple' object does not support item assignment >>> #List example >>> lst=[23 ,89 , 90 ,"New"] >>> lst=90100 #Works fine >>> lst [23, 89, 90100, 'New'] >>> lst.append( 90 ) #Adding new element,Works fine >>> lst [23, 89, 90100, 'New', 90] >>> lst.remove(89) #deleting 89,Works fine >>> lst [23, 90100, 'New', 90]
For more information on how to perform more operation on list visit the link given below.
Link: List methods
The third difference mainly depends on whether you follow it or not.It is not a hard-and-fast rule.
The tuple are mainly use to store heterogenous type of elements.This means a tuple object usually holds all type of values it may be integer ,or string or another tuple or even another list object.
The list usually holds hommogenous type of elements.This means if an object is made to holds only integer then it will contain only integer.
Despite everything you can use tuple or list the way you want.You can still use list to store heterogenous element,nobody cna stop you only you yourself.
>>> tp1=( 45 , 678 , 9100 ,193 ) #homogenous elements,works fine >>> lst=[ 'text' , 89.45 , 346 , tp1] #heterogenous elements,works fine
For now those are the difference between list and tuple worth knowing. If there are any more important difference it will be updated.