# Python Set with Examples

## How to Use the Python Set

In this tutorial, we'll learn what is a set in Python, how to create and delete a set, add and remove items to and from a set, iterate over a set, and so on.

### What is a Set in Python?

In Python, a set is an unordered collection of unique elements. Unlike lists, sets cannot contain duplicate elements.

### Creating a Set in Python

A set is created by surrounding objects with **{ }** and separating the items with commas (**,**).

empty_set = { }

vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot"}

### Accessing Items

It's not possible to access an individual item in a set, since sets are unordered and unindexed.

### Adding Items

To add an item to a set, we use the **add**() method.

The following illustrates an example of adding one item to a set:

vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot"}

vegetables.add("Cauliflower")

print(vegetables)

**Result**:

{'Lettuce', 'Onion', 'Garlic', 'Asparagus', 'Cauliflower', 'Cabbage', 'Carrot', 'Broccoli'}

If you want to add more than one items, you can the **update**() method as you can see in the following example:

vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot"}

vegetables.update(["Cauliflower", "Okra", "Spinach", "Celery"])

print(vegetables)

**Result**:

{'Cauliflower', 'Carrot', 'Lettuce', 'Okra', 'Asparagus', 'Garlic', 'Celery', 'Broccoli', 'Spinach', 'Cabbage', 'Onion'}

As mentioned above, since sets cannot contain duplicate values. Let's take a look at the following example:

vegetables = {"Cabbage", "Lettuce", "Garlic"}

vegetables.add("Cabbage")

print(vegetables)

**Result**:

{'Garlic', 'Lettuce', 'Cabbage'}

As you can see, nothing changes in the set.

### Removing Items

To remove an item from a set, we can use the **remove**() method, or the **discard**() method.

The following illustrates an example of removing items from a set using the remove() and discard() methods:

vegetables = {"Cabbage", "Lettuce", "Garlic", "Onion", "Asparagus", "Broccoli", "Carrot", "Cauliflower"}

vegetables.remove("Cauliflower")

vegetables.remove("Lettuce")

print(vegetables)

**Result**:

{'Cabbage', 'Carrot', 'Broccoli', 'Garlic', 'Asparagus', 'Onion'}

### Deleting a Set

To delete a set, we use the **del** keyword as you can in the following example:

students = {"Student A", "Student B", "Student C"}

del students

print (students) #this causes an error because students is no longer available!

**Result**:

NameError: name 'students' is not defined

### Checking If an Item Exists

To check if an item exists in a set, we can use the **in** keyword.

vegetables = {"Cabbage", "Lettuce", "Carrot", "Cauliflower", "Garlic", "Onion", "Asparagus", "Broccoli"}

if "Carrot" in vegetables:

print ("The item \"Carrot\" already exists!")

if "Chili Pepper" not in vegetables:

print ("The item \"Chili Pepper\" does not exist!")

**Result**:

The item "Carrot" already exists!

The item "Chili Pepper" does not exist!

Note that we use the **not in** keyword to check if an item doesn't exist in a set.

### Iterating a Set

We can use the For loop statement to iterate over a set as illustrated in the following example:

students = {"Student A", "Student B", "Student C", "Student D", "Student E"}

for student in students:

print (student)

**Result**:

Student A

Student C

Student E

Student B

Student D

### Length of a Set

We can use the **len**() method to get the number of elements in a set as illustrated in the following example:

students = {"Student A", "Student B", "Student C", "Student D", "Student E"}

print("The number of items in a set is", len(students))

**Result**:

The number of items in a set is 5

### Copying A Set in Python

We can copy a set using the **copy**() method as illustrated in the the following example:

students = {"Student A", "Student B", "Student C", "Student D", "Student E"}

students_1 = students

students_2 = students.copy();

students.add("Student F")

print ("students_1:", students_1)

print ("students_2:", students_2)

**Result**:

students_1: {'Student E', 'Student F', 'Student C', 'Student D', 'Student A', 'Student B'}

students_2: {'Student E', 'Student D', 'Student A', 'Student B', 'Student C'}

In this tutorial, we've learned what is a set in Python, how to create and delete a set, add and remove items to and from a set, iterate over a set, and so on.