Learn Nested Dictionaries in Python with Netmiko
Автор: Ferds Tech Channel
Загружено: 2024-12-16
Просмотров: 44
Описание:
In this video, we will learn about nested dictionaries in Python. Below is the code I used in the video:
A nested dictionary is a dictionary inside another dictionary.
Key: A unique and immutable identifier (e.g., a string, number, or tuple).
Value: Can be any data type (string, number, list, another dictionary, etc.).
Structure: Key-value pairs are separated by a colon (:), and pairs are enclosed within curly braces ({}).
Separator: Each key-value pair is separated by a comma (,).
from pprint import pprint
Initial Nested Dictionary
device_data = { # Main dict
"Router1": { # Nested dict for "Router1"
"hostname": "R1", # Key-value pair
"ip": "192.168.1.1", # Key-value pair
} # End of nested dict
} # End of main dict
Create: Add new interface
device_data["Router1"]["interfaces"] = {
"Gig0/0": {"status": "up", "ip": "192.168.1.2"},
"Gig0/1": {"status": "down", "ip": None},
}
Read: Access data
print(device_data["Router1"]["hostname"])
print(device_data["Router1"]["interfaces"]["Gig0/0"]["ip"])
Update: Modify existing data
device_data["Router1"]["ip"] = "192.168.1.2"
device_data["Router1"]["interfaces"]["Gig0/1"]["status"] = "up"
Delete: Remove data
del device_data["Router1"]["interfaces"]["Gig0/1"]
del device_data["Router1"]["ip"]
Print final result
print(device_data)
#python #pythonforbeginners #networkautomation
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: