Junior
What is the difference between lists and sets in Python?
sobes.tech AI
Answer from AI
List — an ordered, mutable collection of elements that allows duplicates. Set — an unordered, mutable collection of unique elements that does not allow duplicates.
# Examples
my_list = [1, 2, 2, 3] # List with a duplicate
my_set = {1, 2, 3} # Set, duplicate automatically removed
| Characteristic | List | Set |
|---|---|---|
| Order | Ordered (by index) | Unordered |
| Mutability | Mutable | Mutable |
| Duplicates | Allowed | Not allowed (elements are unique) |
| Access by index | Possible | Impossible |
| Main purpose | Storing a sequence | Checking for uniqueness, set operations |