Python set Data Type

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

By remaining on this website or using its content, you confirm that you have read and agree with the Terms of Use Agreement.

We are not liable for any damages resulting from using this website. Any information may be inaccurate or incomplete. See full Limitation of Liability.

Content may include affiliate links, which means we may earn commission if you buy on the linked website. See full Affiliate and Referral Disclosure.

We use cookies and similar technology to improve user experience and analyze traffic. See full Cookie Policy.

See also Privacy Policy on how we collect and handle user data.

© 2024 PyTut