What is the difference between lists and tuples in Python?

Python, List · Jun 12, 2021

Python's lists and tuples may seem pretty similar in syntax and function, however they have some major differences the most important of which is the fact that lists are mutable and tuples aren't. Here's a quick breakdown:

  • Syntax: [1, 2, 3]
  • Contained elements are mutable (can be changed after creation)
  • Lists have a variable length
  • A list takes up more memory than a tuple
  • Syntax: (1, 2, 3)
  • Contained elements are immutable (cannot be changed after creation)
  • Tuples have a fixed length
  • A tuple takes up less memory than a list

Lists provide a more accessible API and should be used whenever similar types of objects need to be stored and are expected to change over the course of the application's execution. On the other hand, tuples should be used for immutable data, behaving more like constants than variables.

More like this