Nested Dictionary In Python

In today’s lesson we will analyze nested dictionary in python with examples.

Nested Dictionary Python

Nested dictionary in Python includes other dictionaries. A nested dictionary in Python is a dictionary inside a dictionary.

It combines several dictionaries into one. This is known as nested dictionary python.

Make a footballer bio dictionary that includes three footballers’ dictionaries:

Example: 

footballer_bio_dict = { "footballer1" : { "full name": "Edson Arantes do Nascimento(Pelé)", "Place of birth": "Três Corações, State of Minas Gerais, Brazil", "height": "1.73 m (5 ft 8 in)", "position": "Forward", "first club": "Santos", "world cups": 3 }, "footballer2" : { "full name": "Lionel Andres Messi", "Place of birth": "Rosario, Santa Fe, Argentina", "height": "1.70 m (5 ft 7 in)", "position": "Forward", "first club": "FC Barcelona", "world cups": 1 }, "footballer3" : { "full name": "Cristiano Ronaldo", "Place of birth": "Funchal, Madeira, Portugal", "height": "1.87 m (6 ft 2 in)", "position": "Forward", "first club": "Sporting CP", "world cups": 0 } } print(footballer_bio_dict)

If you would like to include three dictionaries in a new dictionary – Make three footballers’ dictionaries, then create your final footballer bio dictionary, which includes all three dictionaries:

Example: 

footballer1 = { "full name": "Edson Arantes do Nascimento(Pelé)", "Place of birth": "Três Corações, State of Minas Gerais, Brazil", "height": "1.73 m (5 ft 8 in)", "position": "Forward", "first club": "Santos", "world cups": 3 } footballer2 = { "full name": "Lionel Andres Messi", "Place of birth": "Rosario, Santa Fe, Argentina", "height": "1.70 m (5 ft 7 in)", "position": "Forward", "first club": "FC Barcelona", "world cups": 1 } footballer3 = { "full name": "Cristiano Ronaldo", "Place of birth": "Funchal, Madeira, Portugal", "height": "1.87 m (6 ft 2 in)", "position": "Forward", "first club": "Sporting CP", "world cups": 0 } footballer_bio_dict = { "footballer1" : footballer1, "footballer2" : footballer2, "footballer3" : footballer3 } print(footballer_bio_dict)

Another Example: 

footballer1 = { "full name": "Edson Arantes do Nascimento(Pelé)", "Place of birth": "Três Corações, State of Minas Gerais, Brazil", "height": "1.73 m (5 ft 8 in)", "position": "Forward", "first club": "Santos", "world cups": 3 } footballer2 = { "full name": "Lionel Andres Messi", "Place of birth": "Rosario, Santa Fe, Argentina", "height": "1.70 m (5 ft 7 in)", "position": "Forward", "first club": "FC Barcelona", "world cups": 1 } footballer3 = { "full name": "Cristiano Ronaldo", "Place of birth": "Funchal, Madeira, Portugal", "height": "1.87 m (6 ft 2 in)", "position": "Forward", "first club": "Sporting CP", "world cups": 0 } footballer_bio_dict = { "footballer1" : footballer1, "footballer2" : footballer2, "footballer3" : footballer3 } print(footballer_bio_dict["footballer1"])

Here are some key points to keep in mind:

  • In a nested dictionary, dictionaries are arranged in an unordered manner

  • Nested dictionaries cannot be sliced.

  • Depending on the situation, we can shrink or grow the nested dictionary.

  • Keys and values are also present in Dictionary.

  • A key is used to access the dictionary.



Python Nested Dictionaries Importance

The copy() method in Python dictionaries is important for several reasons:

  1. When you assign a dictionary to a new variable without using the copy() method, both variables will reference the same underlying dictionary object. Any modifications made to one variable will affect the other. By using the copy() method, you create an independent copy of the dictionary, ensuring that modifications to one copy do not affect the other.
  2. The copy() method allows you to create a backup or snapshot of a dictionary at a specific point in time. This can be useful when you need to preserve the state of a dictionary before performing operations that may modify its contents. You can later refer back to the copy if needed.
  3. In certain scenarios, you may want to work with a duplicate dictionary to avoid unintended modifications to the original data. By using the copy() method, you can create a separate copy of the dictionary that can be modified independently, without affecting the original data.
  4. When passing a dictionary as an argument to a function, using the copy() method ensures that the function operates on a separate copy of the dictionary. This prevents the function from modifying the original dictionary outside of its scope.
  5. The copy() method is useful when you need to iterate over a dictionary and modify its contents at the same time. Creating a copy allows you to avoid potential issues that may arise from modifying the dictionary during iteration, such as changing the dictionary size or altering the order of iteration.
Your input matters to us. Leave your reaction below to acknowledge our efforts or offer valuable suggestions.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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