Python Dictionary Practical Implementation.

Here I am going to explore Python Dictionary Practical Implementation. It is a data structure that stores values in key and value format and we use the “{ }” symbol to create a dictionary in python but you might think what is the real-time use case of python dictionaries so let me give you a real-time example. Most organizations are storing their data in NoSQL DB. This is a storage system in which we store values in JSON format and you can handle and analyze Json files using python dictionaries.

How to create python Dictionary.

color = {1:"red",2:"blue",3:"pink",4:"yellow",5:"green"}

#Here 1 is key and "red" is a value, 2 is key and "blue" is value and so on.

emp = { "id":001,"name":"rakesh","dob":"1987-03-16","dept":"sales"}

#Here id is key and 001 is a value,"name" is key and rakesh is a value and so on.

How to Fetch Dictionary Elements.

print(emp["id"])    #Output 001
print(emp["name])   #Output rakesh
print(emp["dob"])   #Output 1987-03-16
print(emp["dept])   #Output  sales

In the above example emp is dictionary name and “id” is a key .this will return the value that is attached with id key(001)

Python Dictionary Practical Implementation:- Let us see the use of for loop for the same task above.

for key in emp.keys():
    print(key," ",emp[key])
   

This for loop will return all keys and values from emp dictionary .emp.keys() function will return keys one by one like 1st return is “id” that is the first key then it will print key 2nd emp[key] will print value of the same key this process will continue for all keys that are present inside a dictionary.

Dynamic Dictionary: Let us see how we can create a dynamic Dictionary at run time, so here we will take inputs from the users and pass these inputs into the dictionary.

id=int(input("please enter employee id"))
name=input("please enter employee name")
dob=input("please enter employee date of birth")
dept=input("please enter employee department")

emp = {"id":id,"name":name,"dob":dob,"dept":dept}
# Filling dictionary values from the above variables.

for item in emp.items():
   print(item)

#Answer
#("id",001)
#("name","rakesh")
#("dob","1987-03-16")
#("dept","sales")

Nested Dictionary: Let us see how we can create a Nested Dictionary so When a dictionary is present inside another dictionary then this is the nesting of dictionaries. let us understand it by an example.

emp ={
    "id":101,
    "name":"rakesh",
    "dob":"16-03-87",
    "dept":"sales",
    "contact":{1:"8822345432",2:"89876575654",3:"7833432345"}
     }
print(emp)    

How to Fetch Nested Dictionary Elements.

#Style-1
emp["contact"][1]
emp["contact"][2]
emp["contact"][3]

#using for loop
for i in range(1,len(emp["contact"])+1):
   print("contact",i,"=",emp["contact"][i])

How to delete Dictionary Elements.

#deleteing dept key and its value
emp.pop("dept")
emp

# deleting nested dictionary elements
no=int(input("please enter the contact no to be delete"))
print(emp["contact"].keys())
emp["contact"].pop(no)
print(emp)

Jashika Bhatt

Jashika is a professional blogger, self-published author and speaker at techbook and passionate about digital marketing.

Leave a Reply

Your email address will not be published. Required fields are marked *