How to create and use Python set: 7 examples

The set in Python

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 position of the elements 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 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 display its elements:

The code for creating and displaying the set:

# An example of constructing a set



set_ex = {10,20,30,40,50}



for curr_set_item in set_ex:



print ("The set item:",curr_set_item)

Output:

Python 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:

 

The code:

# An example of constructing a set



name_set = {'Tina','Mike','Shaun', 'Nena','Samina'}



print (name_set)

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:

The code:

# An example of constructing a set



name_set = {'Tina','Mike','Shaun', 'Nena','Samina', 'Mike'}



print (name_set)

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:

The code:

set1 = {3,5,8,10,20}

set_compre = {x**2 for x in set1}



print ("The first set:",set1)

print ("A set comprehension:",set_compre)

Output:

set comprehensio

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.

The code:

set_numbers = {3,5,8,10,20}

set_fruits = {'Apple','Banana', 'Orange','Banana'}



print ("The length of first set =",len(set_numbers))

print ("The length of  fruit set = ",len(set_fruits))

Output:

set length

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:

The code for removing elements:

set_fruits = {'Apple','Banana', 'Orange'}



print ("The set before clear method =" ,set_fruits)



set_fruits.clear()

print ("The set after clear method = ",len(set_fruits))

Result:

set clear

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:

The code for testing membership:

# An example of constructing a set



name_set = {'Tina','Mike','Shaun', 'Nena','Samina'}



if ( 'Shaun' in name_set ):

print ("The name exists in set!")

else:

print ("The given name does not exist!")

Output:

set membership

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!