1. Lists (Mutable Ordered)
Lists store multiple items in square brackets []. They're ordered, changeable, and allow duplicates.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Add item
fruits[1] = "blueberry" # Change item
print(fruits) # [apple, blueberry, cherry, orange]
print(len(fruits)) # 4
['apple', 'blueberry', 'cherry', 'orange']
4
2. Tuples (Immutable Ordered)
Tuples use () parentheses and cannot be changed after creation. Perfect for fixed data.
coords = (10, 20, 30)
print(coords[0]) # 10 (indexing works)
print(coords[1:3]) # (20, 30) slicing
# coords[0] = 100 # ERROR! Tuples are immutable
10
(20, 30)
3. Sets (Unique Unordered)
Sets use {} curly braces and store unique items only. Great for membership testing.
colors = {"red", "blue", "red"} # Duplicates removed
print(colors) # {'red', 'blue'} (order not guaranteed)
print("red" in colors) # True (fast lookup)
{'blue', 'red'}
True
4. Dictionaries (Key-Value)
Dictionaries store key:value pairs in {} curly braces. Keys must be unique.
student = {
"name": "Alice",
"age": 20,
"grades": [85, 92, 78]
}
print(student["name"]) # Alice
student["city"] = "NYC" # Add new key
Alice
Quick Comparison
| Structure | Mutable? | Ordered? | Duplicates? | Syntax |
|---|---|---|---|---|
| List | ✅ Yes | ✅ Yes | ✅ Yes | [] |
| Tuple | ❌ No | ✅ Yes | ✅ Yes | () |
| Set | ✅ Yes | ❌ No* | ❌ No | {} |
| Dict | ✅ Yes | ✅ Keys | ❌ Keys | {key:val} |
*Sets maintain insertion order since Python 3.7
Common Methods
List Methods
append(), pop(), remove(), sort()
Dict Methods
get(), keys(), values(), items()
Set Methods
add(), remove(), union(), intersection()
🧠 Data Structures Quiz (8 Questions)
Q1: Which structure allows duplicates?
Q2: What is fruits[1] on ["apple", "banana"]?
Q3: Can you change a tuple item?
Q4: Dict syntax?
Q5: {"a", "a"} has how many items?
Q6: Access dict value: {"name": "Bob"}
Q7: List slicing nums[1:3]?
Q8: Best for unique items lookup?