Python Sets
Sets
Set is a collection of unordered items separated by comma and enclosed within curly brackets.
Sets are unindexed and will not allow duplicate items.
EntitySet = {"Body", "Face", "Edge"}
print(EntitySet)
Output
{'Face', 'Edge', 'Body'}
Use len() method to find the number of items in the set.
Add Items
add()add an item to set.
EntitySet = {"Body", "Face"}
EntitySet.add("Edge")
print(EntitySet)
Output
{'Face', 'Edge', 'Body'}
update()
add multiple items to set.
EntitySet = {"Body", "Face"}
EntitySet.update(["Model", "Edge"])
print(EntitySet)
Output
{'Face', 'Edge', 'Model', 'Body'}
Remove Items
remove()
removes the given item, posts error if item doesn't exist.
EntitySet = {"Body", "Face", "Edge"}
EntitySet.remove("Face")
print(EntitySet)
Output
{'Edge', 'Body'}
discard()
removes the given item, posts error if item doesn't exist.
EntitySet = {"Body", "Face", "Edge"}
EntitySet.discard("Face")
print(EntitySet)
Output
{'Edge', 'Body'}
Delete Set
deletes the set.
EntitySet = {"Body", "Face", "Edge"}
del EntitySet
Clear Set
empties the set.
EntitySet = {"Body", "Face", "Edge"}
EntitySet.clear()
print(EntitySet)
Output
set()
Set Traversal
EntitySet = {"Body", "Face", "Edge"}
for e in EntitySet:
print(e)
Output
Face
Edge
Body
Check Set for an item
EntitySet = {"Body", "Face", "Edge"}
if "Face" in EntitySet:
print("Face")
Output
Face