How to create and use Python set: 7 examples

The set in Python

The Python has a data type for creating sets in its programs. A set is a collection of unordered items that can be created by using curly brackets {}.

By unordered, it means the elements position in a set is not recorded. And so, you may not perform indexing, slicing etc. (the operations that you can perform in sequences)

The collection in a set may not contain duplicate entries. Basically, the set is used for eliminating duplicate entries and membership testing. You may perform the certain mathematical operation in a set as well.

Syntax of creating Python sets

This is how you may create sets, e.g.

name_set = {‘Jake’, ‘Mike’,’Shela’}

So, in the curly braces, items are separated by commas.

You may also create set comprehension just like lists.

aSet = [x for x in iterable]

The set comprehension involves creating a new set where items of iterable (a sequence) go through a certain operation.

In next section, I will show you creating simple sets along with set comprehension examples with code.

A demo of creating and displaying a set

In this example, a set of five unordered numbers is created. After that, a for loop is used to displayed its elements:

Python set

See online demo and code

The code for creating and displaying the set:

 

An example of creating a set of strings

In this example, a set Python is created for storing the names. This is how set is created and displayed:

set strings

See online demo and code

The code:

 

The output of the above code is:

{‘Samina’, ‘Mike’, ‘Shaun’, ‘Nena’, ‘Tina’}

What happens if a set contains duplicate values?

In this example, the name ‘Mike’ is used twice as creating a name set. The next statement uses the print function to display the set items

name_set = {‘Tina’,’Mike’,’Shaun’, ‘Nena’,’Samina’, ‘Mike’}

and see the output:

set duplicate

See online demo and code

The code:

 

The output is the same as in above example, i.e. it removed the duplicate entry:

{‘Samina’, ‘Nena’, ‘Tina’, ‘Mike’, ‘Shaun’}

An example of set comprehension

In this example, two sets are constructed. The first set contains five numbers. The other set is created based on the first set by squaring the values of each item in the first set. See the code and output online:

set comprehensio

See online demo and code

The code:

 

An example of getting length of a set

You may use the len method to get the total unique items in the specified set. It will not include duplicate entries.

See the example below where two sets are constructed and length of each is displayed by the len method. For demo purpose, the second set is given a duplicate entry.

set length

See online demo and code

The code:

 

You can see, although the second set contains four items, however, its length is returned as 3. Because the word “Banana” is used twice in the second set.

An example of clearing set items

You may use the clear method for removing the set items. Just specify the set name with clear method for removing all elements as shown in the example below:

set clear

See online demo and code

The code for removing elements:

 

An example of testing membership

In this example, a member or item of the set is tested whether it exists or not. If a given name exists in the set, it will return true and a message will be displayed accordingly:

set membership

See online demo and code

The code for testing membership: