Interview question
What is the difference between a list and a tuple in Python? - Data Types and Data Structures Python में list और tuple में क्या अंतर है?
Answer
| Feature | List | Tuple |
|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) |
| Mutability | Mutable | Immutable |
| Speed | Slower | Faster |
| Memory | More | Less |
| Dict Key | No | Yes |
my_list = [1, 2, 3]
my_list[0] = 99 # Works - mutable
my_tuple = (1, 2, 3)
my_tuple[0] = 99 # TypeError - immutable
# Tuples as dict keys
my_dict = {(1, 2): 'value'} # Works
my_dict[[1, 2]] = 'value' # TypeError| Feature | List | Tuple |
|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) |
| Mutability | Mutable | Immutable |
Was this answer clear?