πΉ Lists π
β Ordered, β Mutable, β Allows Duplicates
π§ Modify using:
append(),extend(),insert(),remove(),pop()
fruits = ["apple", "banana", "cherry"]π Use when order matters and the data may change.
π§± Tuples
β Ordered, β Immutable, β Allows Duplicates
β‘ Faster than lists, safe from accidental modification
coordinates = (10.0, 20.5)π Use when data should not change (e.g., configuration, coordinates).
πΉ Sets π
β Unordered, β Mutable, β No Duplicates
βοΈ Supports operations like
union(),intersection(),difference()
unique_numbers = {1, 2, 3}π Use for uniqueness, fast membership tests, and removing duplicates.
π Dictionaries π
π Key-value pairs, β Mutable, β No duplicate keys
Fast lookup using keys
student = {"name": "Alice", "age": 21}π Use when associating keys with values (like database records or API responses).
π List vs Tuple vs Set β Quick Comparison
Feature | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
Syntax |
|
|
|
|
Example |
|
|
|
|
Ordered | β Yes | β Yes | β No* | β Yes** |
Mutable | β Yes | β No | β Yes | β Yes |
Allows duplicates | β Yes | β Yes | β No | Keys β, Values β |
Indexing | β Yes | β Yes | β No | β By index |
Key-Value pairs | β No | β No | β No | β Yes |
Can be changed after creation | β Yes | β No | β Yes | β Yes |
Use when | You need an ordered, changeable collection | You need an ordered, unchangeable collection | You need unique items | You need data in key-value form |
β Tip:
Lists for general-purpose arrays and sequences.
Tuples when data must remain unchanged.
Sets when duplicates must be eliminated and membership tested quickly.
Dictionaries when data is associated with named identifiers.