How to make an invested python dictionary

Create nested dictionary in Python with an example: store employee info, names, ages, and job titles.

How to Make an Invested Python Dictionary

Python dictionaries are powerful data structures. They allow you to store and retrieve data quickly, and can be used to represent complex data sets. In this tutorial, we will learn how to create an invested Python dictionary, which is a dictionary that stores multiple values in each key.

We will start by creating a simple dictionary. A dictionary is composed of key-value pairs. Each key is associated with a single value, and each value can be of any data type. For this example, we will create a dictionary with three key-value pairs.

d = {
    "name": "John",
    "age": 30,
    "city": "New York"

Now that we have a simple dictionary, we can start investing it. An invested dictionary is one that stores multiple values for a single key. In the example above, we only have one value for each key. To invest our dictionary, we will create a list of values for each key.

d = {
    "name": ["John", "Jane", "Jim"],
    "age": [30, 25, 35],
    "city": ["New York", "London", "Paris"]

Now we have an invested dictionary. We can access the values for each key using the same syntax we used for the simple dictionary. For example, if we want to access the name of the first person in our invested dictionary, we can use the following syntax:

name = d["name"][0]

This will return the value "John". We can also use our invested dictionary to iterate over the values of each key. For example, if we want to print out the name and age of each person in our dictionary, we can use the following code:

for name, age in zip(d["name"], d["age"]):
    print(f"{name} is {age} years old.")

This code will print out the following:

John is 30 years old.
Jane is 25 years old.
Jim is 35 years old.

In this tutorial, we learned how to create and use an invested Python dictionary. Invested dictionaries are powerful data structures and can be used to represent complex data sets. We hope you found this tutorial helpful!

Answers (0)