Python Collections Explained with Simple Examples (List, Tuple, Set, and Dictionary)
Python has four common built-in collections used to store multiple items: list, tuple, set, and dictionary. Each one is designed for a slightly different situation.
List
A list is an ordered collection that can be changed.
Use it when items may grow, change, or be updated.
fruits = ["apple", "banana", "mango"]
fruits.append("orange")
print(fruits)
Lists keep order and allow duplicates.
Tuple
A tuple is ordered but cannot be changed after creation.
Use it for fixed data.
colors = ("red", "green", "blue")
print(colors[0])
Once created, a tuple stays the same.
Set
A set stores unique values only and does not keep order.
Use it when you want to remove duplicates.
numbers = {1, 2, 2, 3, 4}
print(numbers)
Output will be {1, 2, 3, 4} because duplicates are removed.
Dictionary
A dictionary stores data as key-value pairs.
Use it when items need names instead of numeric positions.
student = {
"name": "Arif",
"age": 25
}
print(student["name"])
Quick idea