Sets Vs Lists Python Programmer Tips

sets vs lists in python

List example

>>> unique = []
>>> for name in ['srini', 'srini', 'rao', 'srini']:
... if name not in unique:
... unique.append(name)
...
>>> unique
['srini', 'rao']

Set example

>>> for name in ['srini', 'srini', 'rao', 'srini']:
... unique.add(name)
...
>>> unique
{'srini', 'rao'}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store