DEV Community

Cover image for Learning Python-Basic course: Day 18, Dictionaries in Python
Aatmaj
Aatmaj

Posted on

4 3

Learning Python-Basic course: Day 18, Dictionaries in Python

Welcome all🤟 Today we will cover dictionaries!


Dictionary is simply a collection of unordered key value pairs
Or sometimes referred as a 'hash table' of key value pairs. Dictionary holds key:value pair. this means that every value in an dictionary is mapped with some other value. Values in a dictionary can be of any datatype. Dictionaries cannot have two items with the same key for obvious reasons.
example

AatmajProfileDictionary={"name":"Aatmaj","Hobby":"teaching","Commits":700}
Enter fullscreen mode Exit fullscreen mode

Here is a quick difference between lists, tuples and dictionaries.
image

a=[] #list
a=() #tuple
a={}#dictionary
Enter fullscreen mode Exit fullscreen mode

Hash table

A Hash table is a data structure. A hash table is a data structure that implements an associative array abstract data type that can map keys to values. A hash table uses a hash function to compute an index also called as the hash code, into a array of buckets or slots, from which the desired value can be found.

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Sample program-

Here is a sample program just to get you started with dictionaries. It is quite easy to understand and commented whenever necessary

>>> hardware={    "Brand": "Dell",    "Model": 2430,    "Year":  "2020"}
>>> print(hardware) #prints the value of the dictionary
{'Brand': 'Dell', 'Model': 2430, 'Year': '2020'}
>>> print(hardware["Model"])
2430
>>> print(hardware.get("Model"))
2430
>>> hardware["Year"]=2021 #Changing the value of the dictionary
>>> print(hardware)
{'Brand': 'Dell', 'Model': 2430, 'Year': 2021}
>>> print(hardware.pop("Model"))
2430
>>> print(hardware)
{'Brand': 'Dell', 'Year': 2021}
>>> hardware["Model"]="Lenovo"
>>> hardware["Year"]=2019
>>> print(hardware.popitem()) #popitem returns the last value entered
('Model', 'Lenovo')
>>> print(hardware)
{'Brand': 'Dell', 'Year': 2019}
>>> for y in hardware:
...     print(y)#Corresponds to each key
...
Brand
Year
>>> for x in hardware:
...      print(hardware[x])#refers to the value
...
Dell
2019
>>> for z in hardware.values():
...      print(z)
...
Dell
2019
>>> hardware.clear() #Cleares the dictionary (not delete)
>>> print(hardware)
{}
>>> print(hardware["Price"])#trying to remove element which is not present
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Price'
Enter fullscreen mode Exit fullscreen mode

Multidimensional dictionaries

Same story needs no explanation!

hardware={
    "LAPTOP":{"Brand": "Dell","Model": 2430,"Year":  "2020"},
    "DESKTOP":{"Brand":"Lenovo","Model":8877,"Warranty": 2},
    "TABLET":{"Brand":"Apple", "price":"3000$"}
}
print(hardware)
print(hardware["TABLET"])
print(hardware["LAPTOP"]["Model"]) #Note the syntax
Enter fullscreen mode Exit fullscreen mode
{'TABLET': {'price': '3000$', 'Brand': 'Apple'}, 'LAPTOP': {'Model': 2430, 'Brand': 'Dell', 'Year': '2020'}, 'DESKTOP': {'Model': 8877, 'Brand': 'Lenovo', 'Warranty': 2}}
{'price': '3000$', 'Brand': 'Apple'}
2430
Enter fullscreen mode Exit fullscreen mode

Exercise-
1) Make a dictionary which contains a list and a tuple. Then append the tuple in the list in the dictionary.

2) Dynamic generation of dictionaries- Write a program to take names of five students and their corresponding marks, put them in an dictionary.
output-

Please enter student name peter
Please enter marks 13
Please enter student name john
Please enter marks 32
Please enter student name pappu
Please enter marks 5
Please enter student name bob
Please enter marks 7
Please enter student name mina
Please enter marks 32
{'peter': 13, 'john': 32, 'pappu': 5, 'mina': 32, 'bob': 7}
Enter fullscreen mode Exit fullscreen mode

Answers will be found here

So friends that's all for this part. 😊For any suggestions please ping me🤠.
Here is my Gmail- aatmaj.mhatre@gmail.com 🤟
Don't forget to follow me on GitHub for updates on the course.😊

Sentry workshop image

Sick of your mobile apps crashing?

Let Simon Grimm show you how to fix them without the guesswork. Join the workshop and get to debugging.

Save your spot →

Top comments (1)

Collapse
 
atharva100 profile image
atharva100

awesome blog!

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay