computer science

What is the difference between a list and a tuple in Python?

ABAditi Banerjee · 12 Asked 14d ago 100 views 1 answer

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

PPPriya Patel ✓ Accepted · 14d ago ▲ 4

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.

Log in to post your own answer or join the discussion.

Discussion (0)

No comments yet — start the discussion.

← Back to all questions