Python Set Complete Tutorial

In this section, we will learn what the Set object is and how to use it in Python.

Set in Python: What are Sets?

Python Set object is an object that acts as a container and allows for other values (other objects) to be stored in it.

There are a couple of notes to be aware of when working with Sets:

  • A Set object doesn’t allow for duplicate element! If we put duplicate elements, only one of them will remain and the rest automatically will be removed.
  • The order of elements we put in a Set object is irrelevant! Basically, this order changes when we try to access elements in a Set. (So the first element we put in a set might not be the first element when we want to access it!)
  • Set elements do not have any index number (also they don’t have any `key` as well). For this reason, we can’t use a pair of brackets `[]` like the way we use in list or tuple objects to access an element of a Set.

Python How to Create a Set: Declaring a Set in Python

In order to create a Set object, we have two options:

  • Using a pair of braces `{}`
  • Using the `set()` function. (In the rest of this section, we will explain this function).

We can use a pair of braces `{}` and then put the elements of a set object in there (each element should be separated using a comma `,`.

Again, a set object won’t take duplicate values. Duplicate elements will be discarded automatically.

Example: create a Set in Python

set1 = {"Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian","Elon", "Kimberly"}

print(set1)

Output:

{'Ellen', 'James', 'Richard', 'Jack', 'Ian', 'Jeremy', 'Omid', 'Kimberly', 'Elon'}

Note that we use the value `Elon` two times for this Set object, but then behind the scene one of them was discarded.

Python Set: Access Set Elements (Items)

As mentioned before, we can’t use index numbers to access the elements of a Set object (Or even keys like the way used in dictionaries) because a Set object does not have neither index numbers nor keys.

The way we access the elements within the body of a set object is by looping through the elements using `for in` loop.

Also, we can use the `in` operator to check and see if a Set object has a specific element in its body or not.

Example: accessing python Set elements

set1 = {"Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian","Elon", "Kimberly"}

print("Omid" in set1)

for element in set1:
    print(element)

Output:

True

Richard

Ellen

Ian

James

Elon

Jack

Jeremy

Omid

Kimberly

How to Make a Set in Python: Python set() Function

The second method of creating a Set object is by using the `set()` function.

This function takes an iterable object as its argument (which includes: list, dictionary, set, string value or any other iterable object available in Kotlin). And it will create a Set object using the elements of the target iterable object.

Example: python Set with set() function

list1 = [1,2,4,6,3,5,23,6,4,0,3,2,2,46,7,4,2,6,7,4,3]

setObject = set(list1)
setObj = set(range(10))

print(setObj)
print(setObject)

Output:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

{0, 1, 2, 3, 4, 5, 6, 7, 46, 23}

Python Set object: adding elements to Set

The simplest way of adding a new element to a set object is by using the `add()` method.

This method takes one argument and that is the element we want to add to the target Set object.

Note: if the argument is already part of the elements of the target Set object, then no action will take place.

Example: adding elements to a set object in python

set1 = {"Ellen","Jack", "Omid","Elon"}

set1.add("James")

set1.add("Richard")

set1.add("Jeremy")

set1.add("Ian")

set1.add(True)

set1.add(20)

set1.add(2.23)

set1.add("Kimberly")

print(set1)

Output:

{'Ian', True, 2.23, 'Kimberly', 'Jeremy', 'Elon', 'Jack', 20, 'James', 'Richard', 'Ellen', 'Omid'}

Note: there are multiple methods that could be used to add new elements to a set object. We covered these methods in the Python Set methods section.

Python Set Length

We can also take the current number of elements in a Set object. To do this, we use the `len()` function.

The `len()` function takes one value as its argument and that is the target Set object.

Note: actually a set object is capable of taking any iterable object as its argument (including list, dictionary, set, string etc.) and it will return the current number of elements in those iterable objects.

Example: python length of Set

set1 = {"Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian","Elon", "Kimberly"}

list1 = [1,2,4,6,3,5,23,6,4,0,3,2,2,46,7,4,2,6,7,4,3]

setObject = set(list1)

setObj = set(range(10))

print(len(set1))

print(len(setObject))

print(len(setObj))

Output:

9

10

10

Python Empty Set

We can also create an empty set in Python.

To create an empty Set in Python, assign an empty pair of braces to a variable, as the example below shows, and that will create an empty Set.

Example: creating an empty Set in Python

set1 = {}

print(len(set1))

Output:

0

Python Set to List

For whatever reason, you might want to convert a Set object into a List object. This is possible and we can use the `list()` function for this purpose.

The `list()` function takes an iterable object and creates a List object using the elements of that iterable object.

So considering the fact that Set objects are also iterable, we can use them as the argument of this function.

Example: converting set to list in Python

set1 = {"Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian","Elon", "Kimberly"}

list1 = list(set1)

print(list1)

Output:

['James', 'Jeremy', 'Omid', 'Elon', 'Ellen', 'Ian', 'Kimberly', 'Richard', 'Jack']

Python in Operator and Set Collection

Using the `in` operator, we can check a Set object to see if it has a specific element in its body or not.

Example: checking if an item exists in a python Set via in operator

set1 = {"Ellen","Jack", "Omid","Elon", "James","Richard","Jeremy", "Ian","Elon", "Kimberly"}

if "James" in set1:
    print("Yes, the value James is in the target set object.")

Output:

Yes, the value James is in the target set object.

Difference between Set and a List in Python

The main difference between a list object and a Set object is the fact that lists can have duplicated elements while in Set object we can’t!

Also, in a List object we can access elements using their index numbers but in a Set object there’s no index to be used for accessing the elements.

Note: in the Python Set Methods, we will explain how to add new elements to a Set object.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies