views:

100

answers:

2

In Python, which data structure is more efficient/speedy? Assuming that order is not important to me and I would be checking for duplicates anyway, is a Python set slower than a Python list?

+6  A: 

It depends on what you are intending to do with it. Sets are significantly faster when it comes to determining if an object is present in the set (as in x in s), but are slower than lists when it comes to iterating over their contents. You can use the timeit module to see which is faster for your situation.

Michael Aaron Safyan
+2  A: 

It depends on what you mean by "checking for duplicates anyway". What percentage of input is unique? Also slower to do what? Try writing (for each of set and list) the code that does what you want to do, and time it. You can then ask here to have your code and timing methodology checked/improved.

John Machin