Lists and Sets

in re Sect 4 Unit 18



Im trying to understand the proper use of Sets vs Lists.



Is the only difference between lists and Sets the fact that sets run/print unique elements?



tnx

Hi Aurelio,



The popular usage of Sets is to group a unique set of elements.



There are also other differences that exist between lists and sets.

One is that is the sets are unordered and the other is that sets can't contain unhashable objects (mutable objects like tuples, lists).



These become very useful when you are comparing two sets of elements and do not care about the order (the position of the element in the set).



When you compare two lists if x in list: it compares with each and every element in the list. When you do the same with a set if x in set: it directly looks for the hash value (which is unique) of x in the set and gives the result.



Both lists and sets cater well in different situations and you need to identify the best case scenerio.



Hope this helps!