0% found this document useful (0 votes)
4 views4 pages

Nested Dictonary

A nested dictionary in Python is a dictionary that contains other dictionaries, allowing for the organization of complex data structures. The document explains how to create nested dictionaries and provides examples, such as a gradebook and a family structure. It also describes the use of the get() method for retrieving values safely from dictionaries, including handling missing keys with default values.

Uploaded by

rj0110865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Nested Dictonary

A nested dictionary in Python is a dictionary that contains other dictionaries, allowing for the organization of complex data structures. The document explains how to create nested dictionaries and provides examples, such as a gradebook and a family structure. It also describes the use of the get() method for retrieving values safely from dictionaries, including handling missing keys with default values.

Uploaded by

rj0110865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dictionary(nested)

In Python, a dictionary can also contain many dictionaries. When we put a dictionary inside another
dictionary, then it is called nested dictionary in Python.

In other words, a nested dictionary is the process of storing multiple dictionaries inside a dictionary.

In the real world of technology, a nested dictionary is useful when we are processing and
transforming data from one format into the other.

 Nested dictionaries can be modified, added, or deleted in the same way as regular dictionaries.

 Python provides powerful methods like get(), items(), and update() that work with nested
dictionaries as well.

Create Nested Dictionary in Python

We can create or define a nested dictionary by putting comma-separated dictionaries within curly
braces. The general syntax to create a nested dictionary in Python is as:

gradebook = {

"Mahika" : {

"Maths" : "A",

"Science" : "A++",

"English" : "A",

"GDP" : 9.8

},

"Ravina" : {

"Maths" : "A",

"Science" : "A",

"English" : "B",

"GDP" : 9.3

},

"Kritika" : {

"Maths" : "B",

"Science" : "C",

"English" : "A",
"GDP" : 8.5

In this example, gradebook is the name of nested dictionary with the dictionary Mahika, Ravina, and
Kritika. Dictionaries Mahika, Ravina, and Kritika are separate dictionaries that contain their key-value
pairs. So, we can say that nested dictionary is a collection of dictionaries into a single dictionary.

Example

myfamily = {

"child1" : {

"name" : "A",

"year" : 2004

},

"child2" : {

"name" : "B",

"year" : 2007

},

"child3" : {

"name" : "C",

"year" : 2011

print(myfamily)

Create three dictionaries, then create one dictionary that will contain the other three dictionaries:

child1 = {

"name" : "Emil",

"year" : 2004

child2 = {

"name" : "Tobias",

"year" : 2007

child3 = {
"name" : "Linus",

"year" : 2011

myfamily = {

"child1" : child1,

"child2" : child2,

"child3" : child3

print(myfamily)

The get() method returns the value for key if key is in the dictionary.

You can also specify the default parameter that will be returned if the specified key is not found.
If default is not specified, it returns None. Therefore, this method never raises a KeyError.

It’s an easy way of getting the value of a key from a dictionary without raising an error.

Parameter Condition Description

key Required Any key you want to search for

A value to return if the specified key is not found.


default Optional
Default value is None.

Basic Examples

get() method is generally used to get the value for the specific key.

D = {'name': 'Bharat', 'age': 25}

print(D.get('name'))

# Prints Bharat

If key is not in the dictionary, the method returns None.

D = {'name': 'Bharat', 'age': 25}

print(D.get('job'))

# Prints None

The default Parameter


If key is in the dictionary, the method returns the value for key (no matter what you pass in
as default).

D = {'name': 'Bob', 'age': 25, 'job': 'Manager'}

print(D.get('job', 'Developer'))

# Prints Manager

But if key is not in the dictionary, the method returns specified default.

D = {'name': 'Bob', 'age': 25}

print(D.get('job','Developer'))

# Prints Developer

You might also like