Table of Contents
Sets in Python are similar to list except that they allow for no duplicate elements inside. Not only that but the collection is un-indexed, meaning there is no concept of indexes at all.
Creating a Set
Creating a set is similar to creating both a list and tuple:
PYTHONcolors = {"red", "white", "blue"}
print(colors)
BASH{'red', 'blue', 'white'}
Sets are defined using curly braces.
Adding Items to a Set
While you cannot change the items inside a set, you can still add new items to the set. To do this, use the add()
method.
PYTHONcolors = {"red", "white", "blue"}
colors.add("orange")
print(colors)
BASH{'orange', 'white', 'blue', 'red'}
If you want to add multiple items to a set at once, Python also offers this functionality using the update()
method:
PYTHONcolors = {"red", "white", "blue"}
colors.update(["orange", "black", "green"])
print(colors)
BASH{'orange', 'white', 'green', 'red', 'black', 'blue'}
Looping through a Set
Here is how you iterate over a set:
PYTHONcolors = {"red", "white", "blue"}
for color in colors:
print(color)
BASHwhite
blue
red
Delete an Item from a Set
In addition to adding an item to a set, you can also remove items from it using the remove
function.
PYTHONcolors = {"red", "white", "blue"}
colors.remove("white")
print(colors)
BASH{'blue', 'red'}
If you want to safely try to remove an item from a set that you're not sure exists in the set or not, use the discard
:
PYTHONcolors = {"red", "white", "blue"}
colors.remove("red")
colors.discard("yellow")
print(colors)
BASH{'white', 'blue'}
Check if Item Exists in Set
You can check if an item exists in a set using the in
keyword:
PYTHONcolors = {"red", "white", "blue"}
print("red" in colors)
print("yellow" in colors)
BASHTrue
False
Set Length
Get the number of items in your set using the len()
function:
PYTHONcolors = {"red", "white", "blue"}
print(len(colors))
BASH3
Combining Multiple Sets
When you have two sets and you want to combine them, Python gives you two options. You can either make a new set or add the items to an existing set. To make a new set entirely, use the union()
function:
PYTHONcolors1 = {"red", "white", "blue"}
colors2 = {"red", "purple", "brown"}
colors3 = colors1.union(colors2)
print(colors3)
BASH{'white', 'brown', 'red', 'purple', 'blue'}
If you don't want to make a new set, and simply want to add the items from one set to another, use the update()
function:
PYTHONcolors1 = {"red", "white", "blue"}
colors2 = {"red", "purple", "brown"}
colors1.update(colors2)
print(colors1)
BASH{'blue', 'purple', 'brown', 'red', 'white'}
- How to Install Node on Windows, macOS and Linux
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- How to deploy a .NET app using Docker
- How to deploy an Express app using Docker
- Getting Started with Sass
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting User Location using JavaScript's Geolocation API
- Creating a Twitter bot with Node.js
- Using Push.js to Display Web Browser Notifications
- How To Create a Modal Popup Box with CSS and JavaScript