what is difference between list and tuple in python?

The differences between tuples and lists is , tuples cannot be changed unlike lists. tuples use parentheses, whereas lists use square brackets.

Python list example

Run the code

Let us change the 3rd list object

output

Python tuple example

Output:

  • We cannot change tuple objects. for example below code would give error.

difference between list and tuple in python

  • List objects are mutable and thus they can’t be used as a key in the dictionary, while tuples can be used as key in dictionary.

  • List objects can be changed while tuples are not changed.
  • Lists are homogeneous while tuples are usually heterogeneous.
  • Lists are for variable length, tuples are for fixed length.

Ref:

http://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples

https://docs.python.org/3/tutorial/datastructures.html