Python set
is a collection of items:
>>> symbols = {'AAPL', 'MSFT', 'NVDA'} >>> type(symbols) <class 'set'>
Its main difference from list
and tuple
is that all items must be unique – there can't be duplicates.
>>> symbols = {'AAPL', 'AAPL', 'MSFT', 'NVDA'} >>> symbols {'NVDA', 'MSFT', 'AAPL'}
Finding a set
item by its index does not work, unlike tuples
and lists
.
>>> symbols[1] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'set' object is not subscriptable