Member-only story

Python Dictionary: Zero to Hero with Examples

Learning dictionary for data science and machine learning applications

--

Photo by Alex Chumak on Unsplash

Introduction

Python scripts or programs are generally impossible to exist without the use of lists and dictionaries. As we know, dictionaries play a huge role in indexing; we all have already been familiar with the basic concept of Dictionaries from the previous articles. But in this article, we’ll be exploring advanced methods like turning a dictionary into a list or two lists with real-life examples. So let’s have a recap…

What is a Dictionary?

Theoretically, a dictionary is to store data in key-value pairs. And these items are accessed using keys and not their positions, unlike lists. The values in a dictionary can be of any type (string, integers, floating-point numbers, etc.).

Syntax:

A dictionary is defined by an enclosed comma-separated list of key-value pairs in curly braces.

D = {(key):(value),(key):(value),(key):(value),(key):(value)}

Example:

Let’s look at an example of student roll numbers. Consider a dictionary with students’ roll numbers and their names. Practically, there might be many students with the same names. Hence it would be very difficult to get the details. This is where the concept of dictionaries can help us. Each name is stored under a number. So when we search for a number, we get all the details of one student without any mess. You can also enter a new record into a dictionary very easily.

Program Example:

#searching for recordsSearch_no = {45:’Meena’, 56:’Raj’, 3:’Ashley’, 6:’Balu’, 20:’Hari’}print(Search_no[3])
print(Search_no[45])
print(Search_no[20])
Output:
Ashley
Meena
Hari
#entering a new recordSearch_no = {45:’Meena’, 56:’Raj’, 3:’Ashley’, 6:’Balu’, 20:’Hari’}Search_no[1] = “Ashi”
print(Search_no)
Output:{45: ‘Meena’, 56: ‘Raj’, 3: ‘Ashley’, 6: ‘Balu’, 20: ‘Hari’, 1: ‘Ashi’}

Operators in dictionary

--

--

Amit Chauhan
Amit Chauhan

Written by Amit Chauhan

Data Scientist, AI/ML/DL, Azure Cloud

Responses (1)

Write a response