Download our e-book of Introduction To Python
Pallavi Dhotre
a year ago
Parameters | Lists in Python | Tuples in Python |
Nature | Mutable or changeable | Immutable or cannot change |
Iteration | Iterating through Lists is time-consuming | Iterating through Tuples is not time-consuming |
use | Good for insertion-deletion | Good for accessing elements |
memory | Requires more memory as compare to Tuples | Requires less memory as compared to Lists |
deletion | The List can delete any particular element | Tuples cannot delete elements rather it can delete a whole tuple |
creation | To create a List we can use the following waysdemo_List = [ ] #empty Listdemo_List = [1,2,3,4] #List with integer valuesdemo_List = ['Ram', 'Sham', 'Siya'] #List of strings | To create a Tuple we can use the following ways#Python Tuple exampledemo= ( ) #empty Tupledemo=(1, ) #Tuple with a single elementdemo= (1,2,3,4) #Tuple with integer valuesdemo= ('Ram', 'Sham', 'Siya') #Tuple of strings |
accessing elements | To get an entry from the List we use index numbers of the List. See the following example for more details:demo_List = ['Ram', 'Sham', 'Siya'] #List of stringsprint (demo_List [0]) # access the first elementprint (demo_List [1]) # access the second elementprint (demo_List [2]) # access the third elementOutput:RamShamSiya | To get an entry from the Tuple we use index numbers of the Tuple. See the following example for more details:demo_List = ['Ram', 'Sham', 'Siya'] #Tuple of stringsprint (demo_Tuple [0]) # access the first elementprint (demo_Tuple [1]) # access the second elementprint (demo_Tuple [2]) # access the third elementOutput:RamShamSiya |