python
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
Output
item_1
item_2
item_3
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?
Similar Posts
- Python collection - List in python
- Python collection - Tuples in python
- Python collection - Dictionaries in python
- Convert pandas DataFrame to python collection - dictionary
- Python Measure the execution time of small bits of Python code with the timeit module
- [Python] Use json.dumps() to pretty-print Python dicts
- [Python] The get() method on Python dicts and its "default" arg