- List vs Tuple
List is mutable whereas Tuple is unmutable. You can change values in a list once it is defined, however for a Tuple, once the values are defined, they cannot be changed / manipulated.
- List
Mutable: Can be modified after creation (elements can be added, removed, or changed).
Ordered: Maintains the order of elements.
Indexable: Allows access to elements by their index.
Dynamic Size: Can grow or shrink as needed.
Syntax: Defined with square brackets, e.g., [1, 2, 3].
Methods: Extensive set of methods for modification and querying (e.g., append(), extend(), remove(), pop(), sort()).
my_list = [1, 2, 3]
my_list.append(4) # [1, 2, 3, 4]
my_list[1] = 5 # [1, 5, 3, 4]
- Tuple
Immutable: Cannot be modified after creation.
Ordered: Maintains the order of elements.
Indexable: Allows access to elements by their index.
Fixed Size: The size is fixed upon creation.
Syntax: Defined with parentheses, e.g., (1, 2, 3).
Methods: Limited set of methods mainly for querying (e.g., count(), index()).
No comments:
Post a Comment