What is the difference between a list and a tuple in Python?
I am learning Python in class 11. Both lists and tuples seem to store multiple items. When should I use a list vs a tuple? My teacher said tuples are immutable but I do not understand why that matters.
1 Answer
List: mutable (can change after creation), uses square brackets []. Tuple: immutable (cannot change after creation), uses parentheses (). Use tuples when data should not change: coordinates (x, y), RGB colours (255, 0, 0), months of year. Use lists when you need to add/remove items: shopping cart, student scores, to-do items. Tuples are slightly faster and use less memory. As dictionary keys, only tuples work (not lists, since keys must be hashable/immutable). Practical rule: if the data is fixed and you want to protect it from accidental modification, use a tuple.
No comments yet — start the discussion.