Lists are ordered, mutable sequences—Python's workhorse like JavaScript arrays. Tuples are ordered and immutable—useful for fixed records and dict keys.
List basics
nums = [1, 2, 3]
nums.append(4)
nums[0] = 10
print(len(nums), nums[-1])
Tuples
point = (3, 4)
x, y = point # unpacking
# point[0] = 5 # TypeError: immutable
Parentheses are optional for tuples—a, b = 1, 2 creates a tuple. Single-element tuples need a trailing comma: (1,).
Important interview questions and answers
- Q: List vs tuple?
A: Lists are mutable and use more memory; tuples are immutable, hashable (if elements are), and signal fixed structure. - Q: How copy a list?
A:list.copy(),nums[:], orcopy.copy—assignment aliases the same list.
Self-check
- Which type can you append to?
- What does negative index -1 mean?
Tip: Use tuples for fixed records (coordinates, DB rows); lists when you need append/pop mutability.
Interview prep
- List vs tuple?
Lists mutable; tuples immutable—tuples can be dict keys when elements are hashable.
- Copy a list?
.copy(), slice[:], orlist()—assignment aliases the same object.