datarekha

How do you reverse a list and remove duplicates in Python, and what are the performance implications of each approach?

The short answer

Reversing a list is O(n) whether you use slice notation or list.reverse(). Deduplication is O(n) with a set conversion but O(n²) if you check membership against a list. Understanding when order must be preserved changes which tool to reach for.

How to think about it

This feels like a simple question but it has a performance angle the interviewer usually wants to hear: the difference between O(n) set-based dedup and the O(n²) loop that most beginners write. And the order-preservation question (set vs dict.fromkeys) distinguishes candidates who know the stdlib idioms from those who guess.

Try all the approaches live

When to use each approach

GoalToolTimeSpace
Reverse in placelist.reverse()O(n)O(1)
Reverse to new listlst[::-1]O(n)O(n)
Dedupe, order irrelevantlist(set(lst))O(n)O(n)
Dedupe, order preservedlist(dict.fromkeys(lst))O(n)O(n)
Dedupe + reverselist(dict.fromkeys(lst))[::-1]O(n)O(n)
Learn it properly Lists

Keep practising

All Python questions

Explore further

Skip to content