Python Set Usage Example

Jun 5, 2019

Initialize Sets

items = {1, 2, 3}

or

items = set([1, 2, 3])

Add to Sets

Add values

items.add(4)

Add sets to sets

items.update({5, 6})

Add list to sets

items.update([7, 8])

Check value in sets

if 5 in items:    pass

Remove values from sets

Remove value

items.remove(8)

Remove sets from sets

items.difference_update({6, 7})

Remove list from sets

items.difference_update([4, 5])

Convert sets to list

list_items = list(items)

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.