Skip to content
Speed Test

Speed Test

Python List vs Set Lookup Speed: Why x in set Beats x in list

· Python list vs set lookup speed | set 2 lakh guna se zyada fast

Searching 1 million numbers in Python: a list checks items one by one (O(n)), while a set hashes straight to the right bucket (O(1) on average). On my Mac: 9.5 ms vs 35 ns. Use a set when you search the same data again and again.

The code

race
nums = list(range(1_000_000))
fast = set(nums)

999_999 in nums   # list
999_999 in fast   # set
Interview SheetPractise the patterns behind interview problemsProblems grouped by pattern, each with the idea first and the code in four languages.