Chrome Extension

Python collection - sets in python

my_set = {"item_1", "item_2", "item_3"}

"""
sets are unordered and unindexed 
so it can not be accessed by using index
"""

for (item in my_set):
    print(item)

my_set.add("item_4") #To add single item to set

my_set.update(["item_4", "item_5"]) #To add multiple items to set

my_set.remove("item_3") #To remove an item

del my_set #To remove set
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Output
item_1
item_2
item_3

Sets are python collections which are unordered and unindexed so you can not access or modify its items using the index. You can iterate through it using the 'for' loop as shown in the code snippet. You can use the below functions for changing sets.

.add(item) - To add an item

.update(items) - To add multiple items

.remove(item) - To remove an item

Was this helpful?
0 Comments