Difference between Tuple and List in python

Difference between Tuple and List in python:

  1. the values of list can be changed any time but the values of tuple cant be changed.
  2. list are for variable length, tuples are for fixed length
  3. list are homogeneous data structures and tuples are heterogeneous data structure.
  4. tuples give a slight performance improvement as values cannot be changed once created, that means any API functions wont be changing tuples.

Examples:

size allocation difference:

a= (1,2,3);
b= ([1,2,3];
a.__sizeof__() #outcome: 24
b.__sizeof__() #outcome: 32

change values:

a = [1,2,3]
b = (1,2,3)
a[0] = 5 #outcome a= [5,2,3]    
b[0] = 5 #outcome: error

dictionary identifier:

a = (1,2)
b = [1,2]
c = {a: 1} # outcome: c= {(1,2): 1}
d = {b: 1} # outcome: error

real time example would be something like this:

say you are recording page number and line number details, with tuple you can record only one page number and line number,whereas list can store multiple “page and line” numbers.