Sets in python

This recipe explains what are sets in python , how to create them and how to add elements to them

Create and append to sets in Python

Hello Pythoneers! In this tutorial, we will be understand one of the interesting datatype in python called sets and how to create them. Sets are nothing but unordered collection of items. Sets can have only unique values and duplicates can not be included in a set. Another important to note about set elements is that sets are immutable. However, A set itself is mutable. For example, we can add or delete elements from them.

Creating a set

Sets can be created in python in different ways such as either using a pair of curly braces or using the set() function as shown below.

    ## Creating a set
    # Using {}
    set1 = {1,2,3,4,5,'ProjectPro'}
    print(set1)
    # Using set() function
    set2 = set([1,2,'ProjectPro'])
    print(set2)

Output

    {1, 2, 3, 4, 5, 'ProjectPro'}
    {1, 2, 'ProjectPro'}
    Traceback (most recent call last):
    File "sets_code.py", line 11, in 
        set3 = {1, 2, [10, 20]}
    TypeError: unhashable type: 'list'

 

Note : To create an empty set, use the set() function. Assigning a pair of empty curly braces to a variable creates an empty dictionary. Sets are mutable but since they are unordered indexing is not an option. But you can still add or remove elements as shown below.

Adding elements to a set

There are bunch of options available to add elements to a set. The add() function can be used to add a single element to the set while the update() function is used to add multiple elements at once to the set.

    ## Adding elements to set
    mySet = {10,20,30}

    # Adding one element
    mySet.add(2)
    print("Adding 2 to mySet ",mySet)

    # Adding multiple elements
    mySet.update([11, 22, 33, 'ProjectPro', 10])
    print("Adding [11,22,33,ProjectPro,10] to mySet ",mySet)
    

 

 

Download Materials

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.