Set in Python

In Python, a set is an unordered collection of unique elements defined by enclosing a comma-separated sequence of elements within curly braces { }.

Here's an example of a set that initializes a set called colors containing four color strings:

colors = {"Red", "Green", "Blue", "Yellow"}

Creating a Set

In Python, a set can be created by enclosing elements within curly braces.

Example:

names = {"Jonny", "Danny", "Kelly", "Jane"}
print(names)

Output:

{'Jonny', 'Danny', 'Kelly', 'Jane'}

In Python, you can also create a set using the set() constructor.

Example:

foods = set(["bread", "butter", "milk", "egg"])
print(foods)

Output:

{'bread', 'egg', 'butter', 'milk'}

Accessing Set Elements

In Python, you can access elements in a set using a for loop or by checking for membership with the in operator.

Here's how you can access set elements using a for loop:

foods = {"bread", "butter", "milk", "egg"}

for f in foods:
    print(f)

The output of the above code is as follows:

butter
egg
bread
milk

Here's how you can access elements of a set checking for membership with the in operator:

foods = {"bread", "butter", "milk", "egg"}

if "butter" in foods:
    print("butter is in the set")
else:
    print("butter is not in the set")

The output of the above code is as follows:

butter is in the set

Adding Elements

In Python, you can use the add() method to add elements to a set.

Example:

names = {"Jonny", "Danny", "Kelly", "Jane"}
names.add("Peter")
print(names)

Output:

{'Kelly', 'Danny', 'Jane', 'Peter', 'Jonny'}

Concatenating Elements

In Python, you can use the update() method to concatenate multiple elements to a set. The update() method accepts a list, tuple, or another set and adds its elements to the set.

Example:

fruits1 = {"apple", "banana", "mango", "grapes"}
fruits2 = {"watermelon", "guava", "cherry"}

fruits1.update(fruits2)
print(fruits1)

Output:

{'mango', 'apple', 'grapes', 'cherry', 'watermelon', 'banana', 'guava'}

In Python, you can also use the union() method to combine all elements of sets.

Example:

set1 = {"bread", "butter", "milk", "egg"}
set2 = {"fish", "meat"}
set3 = set1.union(set2)
print(set3)

Output:

{'bread', 'egg', 'fish', 'butter', 'meat', 'milk'}

Removing Elements

In Python, you can use the remove() method to remove elements from a set.

Example:

foods = {"bread", "butter", "milk", "egg"}
foods.remove("butter")
print(foods)

Output:

{'egg', 'milk', 'bread'}

In Python, the discard() function also removes specified elements from a set.

Example:

foods = {"bread", "butter", "milk", "egg"}
foods.discard("milk")
print(foods)

Output:

{'bread', 'butter', 'egg'}

In Python, you can also use the pop() function to removes the last item from a set. Since, a set items are unordered, so you cannot determined which item will get removed. The pop() functions returns the removed item.

Example:

foods = {"bread", "butter", "milk", "egg"}
removed_element = foods.pop()
print(removed_element)

Output:

milk

Deleting Elements

In Python, you can use the del statement to delete a set.

Example:

foods_set = {"bread", "butter", "milk", "egg"}
del foods_set
print(foods_set)

Output:

NameError: name 'foods_set' is not defined

Emptying a Set

In Python, you can use the clear() method to empty a set.

Example:

foods = {"bread", "butter", "milk", "egg"}
foods.clear()
print(foods)

Output:

set()

Set Length

In Python, you can use the len() function to get the the number of elements present in a set.

Example:

foods = {"bread", "butter", "milk", "egg"}
print(len(foods))

Output:

4

Checking If an Element Exists

In Python, you can use the in keyword to check if an element exists in a set.

Example:

foods = {"bread", "butter", "milk", "egg"}
if "butter" in foods:
    print("butter exist")
else:
    print("butter doesn't exist")

Output:

butter exist