Middle
What are the differences between list, set, and tuple in Python?
sobes.tech AI
Answer from AI
In Python:
-
list — a mutable (changeable) ordered collection of elements. You can add, remove, or modify elements. Elements can be of different types.
-
set — a mutable (changeable) unordered collection of unique elements. Duplicates are not stored. Used for set operations (intersection, union, etc.).
-
tuple — an immutable (unchangeable) ordered collection of elements. Once created, its contents cannot be changed. Used when data immutability is required.
Example:
my_list = [1, 2, 3]
my_list.append(4) # list can be modified
my_set = {1, 2, 3}
my_set.add(2) # duplicate will not be added
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # error, tuple cannot be changed
Main differences — mutability, orderliness, and element uniqueness.