Python Lists vs Tuples: Mutable vs Immutable Data Structures

Python Lists vs Tuples (Mutable vs Immutable) Lists = Mutable (can change) list_1 = ['apple', 'banana', 'orange', 'grape', 'mango'] list_2 = list_1 print(list_1) print(list_2) list_1[0] = 'Watermelon' print(list_1) print(list_2) Output: Both lists change (same reference in memory) --- Tuples = Immutable (cannot change) tuple_1 = ('apple', 'banana', 'orange', 'grape', 'mango') tuple_2 = tuple_1 print(tuple_1) print(tuple_2) # This will cause error tuple_1[0] = 'Watermelon' Error: TypeError: 'tuple' object does not support item assignment Key Insights: List → change allowed Tuple → change not allowed Both allow duplicates Both are ordered #Python

To view or add a comment, sign in

Explore content categories