Sitemap

Python Data Structure: Dictionaries

3 min readNov 3, 2022

--

Hello, I am Şeyma, one of the participants of the SistersLab Women in Tech Academy project supported by Toplum Gonulluleri Vakfı (https://www.tog.org.tr/en/).

The project aims to empower people between the age of 20–28 who identify as women, through 3 months long software training and supporting training to increase their participation in the workforce in the IT sector.

You can access detailed information about the project using this link: https://sisterslab.co/women-in-tech-academy/

I will share with you what I learned about python as part of this beautiful project. I wish you good reading.

Press enter or click to view image in full size

Dictionaries from python data types will be our topic this week.

To talk about the dictionaries in Python in the most basic way, it would not be wrong to say that they are different from the dictionary logic that we have all seen in real life. While creating a dictionary in python, we assign a descriptive word key and the corresponding word value. this situation is just like the dictionary logic that everyone knows.

I will use Turkish and English words to give a simple example.

The key word, which is “kitap” in Turkish, is value, which means “book”.

How to create dictionaries:

There are two ways to create a dictionary in python.

The first one is the dictionary you created by putting the “=” sign in front of the dictionary name and continuing with “{}” after you are sure that the dictionary name is not a dict and you put it by following the name rules.

In the second method, you can create your dictionary by using the dict keyword after the “= “ sign you put in front of your dictionary name and ending with ({}) signs.

It is possible to create an empty dictionary.

Things to consider when creating a dictionary:

Get İclal Şeyma Koca’s stories in your inbox

Join Medium for free to get updates from this writer.

* Key value pair must be written inside the {} brackets.
* It is necessary to use “,” after each key-value pair.
* The “:” sign should be used between the key-value pair.

Key value values ​​kept in dictionaries do not have to be in the str format, they can also hold different types of data.
Dictionaries can be added and subtracted.
Key words must be unique but value words can be duplicates.

Access to Dictionary Elements:

It can be accessed by giving the key value in [] or it is possible to access the element by giving the key value in get().

Adding an Element to the Dictionary:

Assignment can be done with the key-value assignment process. If there is no key, it adds as a new assignment, and if it does, it updates the value.
In addition, an element can be assigned to the dictionary with the update() method.

Delete Operations:

You can use the methods listed below for the value you want to delete from the dictionary.

  • del key
  • clear()
  • del dict_name
  • popitem()
  • pop(key)

Dictionary Copy operations

The last thing I will talk about in dictionaries is the copying process and you can copy dictionaries using the following methods.

  • copy()
  • dict()
  • = operator

You have come to the end of my article about dictionaries. I hope you had fun reading it. See you in my next story.

Press enter or click to view image in full size

--

--