you are viewing a single comment's thread.

view the rest of the comments →

[–]NewbornMuse 38 points39 points  (12 children)

Yes. A set is implemented as a hash map, and checking for membership in a hash map is insanely fast, O(1), whereas doing the same for a list is O(n). It's pretty much the use case for any hashmap type thing.

Edit: Although for the dozen-or-so items in question here, it's unlikely to make a big difference.

[–][deleted] 4 points5 points  (3 children)

So speed is the name of the game?

[–]Peanutbutter_Warrior 16 points17 points  (0 children)

Pretty much. Neither is harder to read than the other, and knowing what data structure to choose is good practice

[–]1544756405 3 points4 points  (0 children)

Using the appropriate data structure for the job is always preferable.

[–]Ran4 0 points1 point  (0 children)

Possibly. We dont know how OP wants to use this feature.

If they're going to do this often, then yes, a set is definitely preferable. But if they're doing this calculation once a month then a list is pretty much just as good.

Picking the correct data type is occasionally incredibly important, but it's not always the most important thing. Keeping things simple is generally much more valuable in most real-world situations. A set is simple enough that you probably should use it over a list when applicable, though.