Python Sets

A set is a collection which is un-ordered & non-indexed.

####Creating a Set

my_set = {"orange", "mango", "apple"}

print(my_set)

Accessing Set Elements

#### Example, looping in set to access element
my_set = {"alpha", "beta", "charlie"}
for element in my_set:
  print(element)
#### Check if element is present set or not
my_set = {"alpha", "beta", "charlie"}
print(“alpha” in my_set)

Adding Element

## Adding element using add()
my_set = {"alpha", "beta", "charlie"}
my_set.add(“delta”)
print(my_set)
### Add multiple items to a set, using the update() method
my_set.update(["one", "two", "three"])
print(my_set)

Removing Element

## Removing element using remove()
my_set = {"alpha", "beta", "charlie"}
my_set.remove(“beta”)
print(my_set)
### Removing element using discard()
my_set = {"alpha", "beta", "charlie"}
my_set.discard(“beta”)
print(my_set)
### Length
print(len(my_set))