There's a huge difference.
- Sets can't contain duplicates
- Sets are unordered
- In order to find an element in a set, a hash lookup is used (which is why sets are unordered). This makes
__contains__
(in
operator) a lot more efficient for sets than lists.
- Sets can only contain hashable items (see #3). If you try:
set(([1],[2]))
you'll get a TypeError
.
In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order.
Also note that if you don't care about order, etc, you can use
new_set = myset.intersection(mylist)
to get the intersection between a set
and a list
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…