How to create a set in Python

About Python Set

In Python, set is an unordered collection of unique elements.

Sets are mutable which means that you can add or remove items from them.

In Python, set elements are placed inside curly braces '{}' and elements are separated with commas.

Creating a set in Python


Using curly braces '{}':

1. Create a set of string (collection of fruit names like "Apple", "Banana", "Cherry", "Orange") separated with commas (,) and place them inside curly braces. The code will look like this: {"Apple", "Banana", "Cherry", "Orange"}. A set of string is created.

2. Now create a variable name fruit_set and assign the set of string to the fruit_set variable. Use print() function to print the set.

Please take a closer look at the following example:

Example of creating set

Output of the above example
The fruit set is: {'Banana', 'Orange', 'Cherry', 'Apple'}


Using the set() constructor:

You can create a set by passing an iterable like list or tuple to the set() constructor.

Set from list:

1. Create a list of integer numbers [1, 2, 3, 4, 5] and pass it to the set() constructor like this - set([1, 2, 3, 4, 5]). It will create a set of integer numbers.

2. Create a variable name num_set and assign the set to the num_set variable. Use print() function to print the set.


Set from tuple:

1. Create a tuple (1, 2, 3, 4) and pass it to the set() constructor like this - set((1, 2, 3, 4)). It will create a set of integer numbers.

Please take a closer look at the following examples:

Example of creating set using set() constructor

Output of the above example
Set from list: {1, 2, 3, 4, 5}
Set from tuple: {1, 2, 3, 4}

Create empty set:

1. Create a variable name empty_set and assign only the set() constructor to the empty_set variable as shown in the following example. Use print() function to print the empty set. It will print 'set()'.

You can add elements to the empty_set object using add() method. The following code empty_set.add(3) and empty_set.add(4) add 3 and 4 to the empty set.

Please take a closer look at the following examples:

Example of creating empty set using set() constructor

Output of the above example
Empty set: set()
Elements added to empty set: {3, 4}


Using set comprehension

You can create a set in a concise way based on existing iterables.

In the first example, a set is created from a range(6).

In the second example, a set is created from a list.

Example of creating set using set comprehension

Output of the above example
Set comprehension from range: {0, 1, 2, 3, 4, 5}
Set comprehension from list: {1, 2, 3, 4}

Live Code Playground

In the following Python code editor, you can practice the above code by creating more set using different methods. Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output