Dictionaries and Sets in Python

Image for post
Image for post

In this article, I would like to share my knowledge about the dictionary and Sets in Python. In the last article, I discussed the common data structures that are widely used in the python community. Here is a continuity of the last article.

Dictionaries

dictionaries are a data structure which is used to store data in the mode of a key: value pair. Each key has its own respective value pair. Since the dictionaries are similar to a map. There are various methods that can be implemented with dictionaries.

Starting with the implementation, to create a dictionary we have to start with {}. Dictionaries are case sensitive, changeable and do not allow duplicates.

Image for post
Image for post
creation of a Dictionary (source: w3schools).

The methods that can be used with the dictionary.

Add a string- thisdict[‘country’] = “UK”

clear() -clears all the items in the dictionary.

copy() -copies and return a copy of the dictionary.

update() -updates the existing key with the latest value pair

check an item- ‘brand’ in thisdict and it returns true and false if it is not present in the dictionary.

length()- can get the length of the dictionary.

There are many more functions that are available for the dictionary and check out this website for more methods here.

Sets

Sets are data structures that are unchangeable, unordered and does not allows duplicate data. The sets output the items in every order when you try to run each every time. when a set is declared you cannot add another item inside the set but you can add different data types into a set.

Image for post
Image for post
Sets in Python (source:w3schools)
Image for post
Image for post
Sets in Python (source-w3 schools)

The Sets in python is similar to the sets in mathematics. we can perform the operations such as union, intersection by just adding and subtracting one set from another. There are methods in sets similar to the dictionary to perform actions.

type()- Returns the type of set

clear()- clears all the items in the sets.

set()- performs the actions as a set constructor

To check more on sets I suggest you go here and here.

I hope this would be sufficient about the Sets and dictionary for a beginner to get to know about the data structure in python. to know more about sets and dictionary go to the links mentioned above. In the next article, I will share a new topic in python until then cheers to all!!!

NOT TODAY!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store