Sets in Python

Sets are unordered collection of items in Python, it doesn’t contain duplicate objects and also objects contained by a set need to be immutable. But Set itself is mutable. For example – { 18, 92, 19 } is a Set in Python.
If you’ve taken Mathematics in your High School then you may remember Sets, If you don’t then see below picture may be it will refresh your memory and makes clear inside your mind What are Sets?

One of benefit of having Sets in Python is that these can be used for modelling real life problems in form of code and then can also be used for Analysis as well. Like Sets in above picture can contain data from a Programming Language Survey and then using Python Sets survey data can be modelled into code, then using Python’s Data Visualisation libraries like ggplot graphs can be made. Which can be really really insightful/helpful for understanding survey data. This is one of application of Sets. There does exist many other practical scenarios which can be dealt with using Python Sets.
Let’s now get to question – How to create sets in Python?

2 Ways to create sets in Python?

Sets can be created in two ways – one being just using Curly Brackets and other being using set() function.

Creating Sets using Curly Brackets in Python
Putting comma separated elements inside curly brackets makes a Set in Python. For example – For creating a Set containing 18, 29, 17 numbers we just need to put curly brackets around this so { 18, 29, 17 } is a Python Set.
Below are some more examples of How Sets can be created in Python using Curly Brackets?

# Creating a set containing numbers
set1 = {42,292,12,39,21}

# Crating a set containing strings
set2 = {"ComputerScienceHub", "Programming", "Python", "C++"}

# Creating a set containing mixed type of objects
set3 = {34, "Hello", 13, "I loves Programming", [27, 91, 18, "I am learning"]}

# Creating a set containing sets
set4 = {{12, 14, 23}, 192, 134, {"Hello", "world"}, 134}

Making Sets in Python using set() function
Similar to list() function there does exist set() function which can be used for creating sets in Python. Here set() expects a single argument which can be list/string or any other Python Data Type, but in case if no argument is passed then this function will return an empty set. For example – set({ 18, 29, 182, 192 }) will return a Python set like this { 18, 29, 182, 192 } (So Simple!!!).
Below are some more examples showing Use of set() function for creating Python Sets?

# Creating a set containing numbers
set1 = set({12,3452,1242,3921,2211})

# Crating a set containing strings
set2 = set({"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"})

# Creating a set containing mixed type of objects
set3 = set({1334, "Hello, How are you?", 13, "I want to be a Doctor", [272, 921, 218, "I am driving now"]})

# Creating a set containing sets
set4 = set({{1132, 314, 923}, 1192, 1344, {"Hello", "world"}, 1324})

# Creating an empty set using Python's set() function
set5 = set()

Accessing Elements inside a Set

As Python Sets doesn’t support indexing so directly accessing element from a set is quite difficult. Like if for a Set x you do x[2] then it will raise TypeError: ‘set’ object is not subscriptable. Meaning that you cannot use square brackets [] to access elements inside Set.
So if usual method of using square brackets cannot be used then How elements inside a Set can be accessed? Simply by converting Set to list first and then using square brackets for accessing element inside list.

  1. If you have a Python Set set1 = { 29, 13, 26 } then change it to a list
  2. Changing Set to List => list1 = list(set1), here list1 = [26 ,29, 13]
  3. Using Square Bracket access value inside list1 like list1[0], list[1] and so on
  4. If list1[some number] is returning an element then it means that element is also in set1

That’s how you can check whether some element is there in a Set or not.

Some other Stuff about Python Sets

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts