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
nums = list(range(1_000_000))
fast = set(nums)
999_999 in nums # list
999_999 in fast # set


![Animated sliding window over the array [2, 1, 5, 1, 3, 2]: a three-number window adds the new number and subtracts the old one, showing sums 8, 7, 9 and 6 and a maximum of 9.](https://i.ytimg.com/vi/NM5paB6_7wg/maxresdefault.jpg)