set vs dictionary python with Examples

In this tutorial you will learn about what is set and dictionary as well as the important difference between set vs dictionary python.

Set and Dictionary both are data types in python used for storing numbers of values but using different method. Set data type store it value in curly braces and separate the items by commas where dictionary also stored it value in curly braces in the form of key and value pairs and items of dictionary is also separated it’s values by using commas.

Now let us see each set and dictionary one by one and then their diffrence.

Set in Python

Set is one type of data types in python which is unordered, unindex & unchangable means items of set are not in order and you cannot change the value of set once declared. Set is mostly used form mathematical task like union,intersection, difference and symmetric. The each item of set is unique from each other.

Examples:

#set of integer values
myset = {1, 2, 3}
print(myset)

# set of mixed datatypes
myset = {1.0, "Hello", (1, 2, 3)}
print(myset)

Dictionary in Python

Dictionary is data types in python which is ordered & changable means items of set are in order and you can change the value of dictionary once declared. Each item of dictionary is separated from each other by commas.

# empty dictionary
mydict = {}

# dictionary with integer keys
mydict = {1: 'one', 2: 'two'}

# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}

# using dict()
my_dict = dict({1:'one', 2:'two'})

Difference between set vs dictionary python

Set                      Dictionary
1. Set is homogenous data types stored value in curly braces.1.Dictionary is homogeneous data types stored value in curly braces inf form of key:value
2. set is created by using set() function.2.Dictionary is created by using dict() function.
3. Set of every items is unique3. Dictionary of every key is unique
4.set is unordered4.Dictionary is ordered
5.Example:{1,2,3,4,5}5.Example:{1:”one”,2:”two”}
6. Creating a set
a=set()  
6.Creating an empty
dictionary d={}  
7. Set is used to join operation and find unique values.7. Dictionary is used to create a data frame with lists as well as used in JSON
Was this article helpful?
YesNo

Leave a Comment