It depends on how you define “Best”. If you’re looking for readings, this is clear
in Is the Best Here. What if you’re looking for performance or memory?
Also, we should not limit ourselves to just one type of data when using
in . We’ll look at
tuple , And
Compare execution times
I’m going to use timeit Module for measuring execution times. We are going to compare 4 different uses. So it’s going to be 1 (
or) Compared to 3 (
tuple , And
The order will be the same as the rest of the article:
in  - list
in () - tuple
in - set
And I will execute everyone
100000 times. In addition, I decided to rename the HTTP method of the request
method Because it is shorter.
Although I made the statements
100000 Sometimes, it’s still hard to decide which is better, but we can say that tuples are a little better.
globals Parameter inside
timeit.timeit Used to access variables defined in the global namespace. In this way, we do not estimate the time required for allocation
"GET” Value for
method variable. Otherwise, we need to change it:
print(timeit.timeit('method = "GET"; method in ["GET", "HEAD", "OPTIONS"]', globals=globals(), number=100000))
What if we want to evaluate the worst case scenario. At this point, the last condition will hold in the first place.
Now, sets are an advantage and
or Shows the worst performance.
Compare memory uses
We can use
sys.getsizeof() To calculate the memory consumption of objects.
In terms of memory, tuples are better than other data containers. However, when we use
or We do not create any sequence then
or He is the winner of the competition for memory!