How to Add Elements to a Python Set

5/5 - (7 votes)

Problem Formulation and Solution Overview

This article will show you how to add elements to a Python set.

ℹ️ Info: A Python set is a collection of unique item(s) saved in no particular order (unordered). A set cannot be changed. However, elements can be added and removed. Sets can also perform calculations like union, intersection, and much more!

To make it more interesting, we use the following practical scenario in this tutorial:

PetBoutique specializes in custom dog breeds. They pride themselves on naming their dogs unique names. They think that by doing this, sales will increase significantly. All dog names are saved to a Python set to ensure no duplications occur.


πŸ’¬ Question: How would we write code to add elements to a set?

We can accomplish this task by one of the following options:


Method 1: Use add()

This method uses the add() function to add a single element to an empty set.

dog_names = set()
dog_names.add('Chewie')
print(dog_names)

The first line in the above code declares an empty set. This saves to dog_names.

The following line uses the add() function and passes it one (1) argument. In this case, a string ('Chewie'). This action adds the element to the dog_names set.

On the next line, the contents of dog_names is output to the terminal.

{'Chewie'}

No error is displayed if we attempt to add the same element again. However, the second occurrence is ignored.

dog_names = set()
dog_names.add('Chewie')
dog_names.add('Chewie')
print(dog_names)

As expected, the contents of dog_names remain the same.

{'Chewie'}

Method 2: Use update() with list

This method uses the update() function in conjunction with list to add a list of items to a set.

sold_yesterday = ['Griffin', 'Tinsley', 'Watson']
dog_names.update(sold_yesterday)
print(dog_names)

This example builds on the existing dog_names set from Method 1.

In this regard, a list of all dogs sold yesterday at PetBoutique has been created. The results save to sold_yesterday in a list format.

The following line uses the update() function and passes it one (1) argument, the list, sold_yesterday and converts this list to a set.

On the next line, the contents of dog_names are output to the terminal.

{'Griffin', 'Watson', 'Tinsley', 'Chewie'}

As expected, the dog_names set now contains four (4) dog names in no particular order.

Python Set update() - Examples, Explanation, Runtime

Method 3: Use the Pipe Operator

This method uses the Pipe Operator (|) to add a new set to an existing set.

sold_today = set()
sold_today.add('Scout')
all_sales = dog_names | sold_today
print(all_sales)

This example builds on the existing dog_names set from Method 1 and Method 2.

In this regard, a list of all dogs sold today at PetBoutique πŸ•πŸ•β€πŸ¦ΊπŸ© is created. The results save to sold_today.

On the following line, a new dog name is added to the sold_today set.

Next, the two (2) sets are joined using the pipe (|) operator. The results save to all_sales.

On the following line, the contents of all_sales are output to the terminal.

{'Scout', 'Chewie', 'Griffin', 'Tinsley', 'Watson'}

As expected, the dog_names set now contains five (5) dog names in no particular order.

Python Set union() - Examples, Explanation, Runtime

Method 4: Use union

This method uses the union() method to add a new set to an existing set.

last_week_sales = {'Scout', 'Chewie', 'Griffin', 'Tinsley', 'Watson'}
this_week_sales = ['Axel', 'Pixie', 'Gunner']
all_sales = last_week_sales.union(this_week_sales)
print(all_sales)

The first line in the above code snippet declares a new set (different from Method 1). The results save to last_week_sales.

The following line declares a list containing dog sales this week. The results save to this_week_sales.

The next line merges the two (2) sets using the union function. The results save to all_sales.

On the following line, the contents of all_sales are output to the terminal.

{'Griffin', 'Watson', 'Tinsley', 'Scout', 'Chewie', 'Pixie', 'Axel', 'Gunner'}

As expected, the dog_names set now contains eight (8) dog names in no particular order.

[Overview] Python's In-Place Assignment Operators | Compound Operators

Summary

This article has provided four (4) ways to add elements to a set to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programming Humor