Interview Question

Remove duplicates from list

dict.fromkeys removes duplicate hashable items while preserving first-seen order.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

Duplicate removal must state whether original order should be preserved. • dict.fromkeys preserves first-seen order for hashable values. • A set removes duplicates but should not be used when list order matters. • Unhashable values require another comparison strategy.

💡 Simple Example

values = [3, 1, 3, 2, 1] unique = list(dict.fromkeys(values)) print(unique)

Output

[3, 1, 2]

⚡ Quick Revision

dict.fromkeys removes duplicate hashable items while preserving first-seen order.