🐍 Lesson 57: Remove Duplicates (Lists & Sets in Python)
1. Problem Overview
In short → If a Python list has repeated elements (duplicates), you'll often want to keep only one of each.
English → Removing duplicates means keeping only unique elements in a list.
2. Methods to Remove Duplicates
set()→ fastest, but loses orderdict.fromkeys()→ preserves order- Loop method → beginner-friendly, preserves order
3. Summary
✅ set() → removes duplicates (order not preserved)
✅ dict.fromkeys() → removes duplicates + preserves order
✅ Loop method → order-preserving duplicate removal
python
# ===== 1. Using set() (Fastest) =====
numbers = [1, 2, 2, 3, 4, 4, 5]
unique = list(set(numbers))
print(f"Using set(): {unique}")
# ===== 2. Preserve Order (Using Loop) =====
print(f"\n===== Preserve Order =====")
numbers = [1, 2, 2, 3, 4, 4, 5]
unique = []
for n in numbers:
if n not in unique:
unique.append(n)
print(f"Using loop: {unique}")
# ===== 3. Using dict.fromkeys() =====
print(f"\n===== Using dict.fromkeys() =====")
numbers = [1, 2, 2, 3, 4, 4, 5]
unique = list(dict.fromkeys(numbers))
print(f"Using dict.fromkeys(): {unique}")
# ===== 4. Using List Comprehension =====
print(f"\n===== List Comprehension =====")
numbers = [1, 2, 2, 3, 4, 4, 5]
unique = []
[unique.append(x) for x in numbers if x not in unique]
print(f"Using list comprehension: {unique}")
# ===== 5. Real-World Example =====
print(f"\n===== Real-World Example =====")
usernames = ["sai", "aye", "sai", "mya", "aye"]
unique_users = list(dict.fromkeys(usernames))
print(f"Original: {usernames}")
print(f"Unique: {unique_users}")You should see
Using set(): [1, 2, 3, 4, 5] ===== Preserve Order ===== Using loop: [1, 2, 3, 4, 5] ===== Using dict.fromkeys() ===== Using dict.fromkeys(): [1, 2, 3, 4, 5] ===== List Comprehension ===== Using list comprehension: [1, 2, 3, 4, 5] ===== Real-World Example ===== Original: ['sai', 'aye', 'sai', 'mya', 'aye'] Unique: ['sai', 'aye', 'mya']