0% found this document useful (0 votes)
1 views

Python Dictionaries_ A Network Engineer’s Guide — Python for Network Engineer

Uploaded by

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

Python Dictionaries_ A Network Engineer’s Guide — Python for Network Engineer

Uploaded by

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

Python Dictionaries: A

Network Engineer’s
Guide
Contents
Python Dictionaries: A Network Engineer’s Guide
Similarities with Lists: Mutability
Using Curly Braces to Create a Dictionary
Navigating Dictionaries in Python
Dictionary Toolbox: Methods
Exploring Keys, Values, and Items
Dynamic Modifications with .pop()
Deletion Strategies: del and update
Dictionary Iteration Techniques
Nested Dictionaries in Python

Dictionaries in Python are like special containers that store data as


key-value pairs. They keep the order of items and are very useful
for many programming tasks. You can create dictionaries using
stable3.7
curly braces {} and access values by their keys. From Python
onwards, dictionaries are ordered by default, making them even
more powerful for writing organized code.
Think of dictionaries as magical containers that hold key-value
pairs. Each key is linked to a specific value, making it easy to
organize and retrieve information. Here’s how you create one:

my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'val


# Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'val

 

In this example, 'key1' is linked to 'value1' , 'key2' to


'value2' , and so on. The curly braces {} enclose the dictionary,
making it a powerful tool for Python programmers.

One of the best things about dictionaries is their flexibility. You


can use different data types for both keys and values. Unlike some
other programming languages, Python allows you to use a wide
range of data types, including strings, integers, and even other
dictionaries.

mixed_dict = {'name': 'John', 'age': 25, 'grades': {'math':


# Output: {'name': 'John', 'age': 25, 'grades': {'math': 90

 

This flexibility makes dictionaries very versatile, allowing them to


hold various types of information within a single data structure.
Like lists, dictionaries in Python are mutable. This means you can
change them after creating them by adding, removing, or
modifying key-value pairs. This flexibility makes dictionaries great
for handling changing datasets and adapting to new
requirements. stable
A Python dictionary is made up of key-value pairs, enclosed in
curly braces {} , and can handle various data types.
The easiest way to create a dictionary is by using curly braces. You
define key-value pairs within these braces. Here’s an example:

my_dict = {
"rtr1": "10.100.1.2",
"rtr2": "10.100.2.1",
"rtr3": "10.100.3.1",
}
# Output: {'rtr1': '10.100.1.2', 'rtr2': '10.100.2.1', 'rtr

 

In this example, my_dict is a dictionary with router names as keys


and their corresponding IP addresses as values.

Using the dict() Constructor


Python also provides the dict() constructor for creating
dictionaries. This method is flexible and can handle various input
formats. Here’s an example:

alt_dict = dict(rtr1="10.100.1.2", rtr2="10.100.2.1", rtr3=


# Output: {'rtr1': '10.100.1.2', 'rtr2': '10.100.2.1', 'rtr

 

In this example, we use keyword arguments within the dict()


constructor to create the same dictionary as before. The keys
( rtr1 , rtr2 , rtr3 ) and values ( 10.100.1.2 , 10.100.2.1 ,
stable
10.100.3.1 ) are passed directly to the constructor.
Choosing between curly braces {} and the dict() constructor
depends on your needs. If you have fixed values, curly braces are
quick and easy. If you need more flexibility, like creating
dictionaries from variables, use the dict() constructor. Pick the
method that best suits your coding needs.
In Python, dictionaries are powerful tools for storing and
managing data. They use keys to uniquely identify values, making
it easy to access and manipulate elements. Here’s a simple
example:

# Accessing the value for the key "rtr3"


value = my_dict["rtr3"]

If you try to access a key that doesn’t exist, you’ll get a KeyError .
To avoid this, use the get() method, which returns None if the
key is missing:

# Using the get() method to handle missing keys


value = my_dict.get("rtr4")

Dictionaries are not just for retrieving values; you can also update
them. To change the value of an existing key, simply reassign it:

# Assigning a new IP address to the key "rtr3"


my_dict["rtr3"] = "10.100.4.1"

Dictionaries are very efficient for looking up values, even in large


datasets, thanks to their underlying hash table implementation.
stable

You can also add new key-value pairs to a dictionary easily:


# Adding a new key-value pair
my_dict["rtr4"] = "10.100.5.1"

And if you need to update an existing key-value pair, it’s


straightforward:

# Updating the IP address for an existing router


my_dict["rtr3"] = "10.100.4.1"

To remove a key-value pair, use the del keyword:

# Deleting a key-value pair


del my_dict["rtr2"]

While dictionaries are mutable, meaning you can change their


contents, the keys themselves must be immutable. This means you
can’t use a list as a key, for example, because lists can change.
Python dictionaries come with many useful methods for working
with their data. These methods help you extract, manipulate, and
manage dictionary contents.

1. keys() , values() , and


items()
These methods let you look inside a dictionary:

keys() : Gets a list of all keys. stable


values() : Gets a list of all values.

items() : Gets a list of key-value pairs (tuples).


# Example Usage
all_keys = my_dict.keys()
all_values = my_dict.values()
key_value_pairs = my_dict.items()

These methods are useful for iterating over or analyzing the


dictionary’s contents.

The ** .pop() Method


The .pop() method retrieves and removes an item from the
dictionary:

# Example Usage
value = my_dict.pop("rtr1")

This method gets the value for the specified key and removes the
key-value pair from the dictionary.

The ** del Keyword


The del keyword deletes a key-value pair from the dictionary:

# Example Usage
del my_dict["rtr2"]

stable
The ** update() Method
The update() method merges dictionaries and updates existing
keys with new values:

# Example Usage
new_data = {"rtr3": "10.100.4.1", "rtr4": "10.100.5.1"}
my_dict.update(new_data)

If there are overlapping keys, the values in my_dict will be


updated.
When you loop through a dictionary, you iterate over its keys by
default:

# Example Usage
for k in my_dict:
print(k)

This loop prints each key in the dictionary. To iterate over values,
use the .values() method:

# Example Usage
for v in my_dict.values():
print(v)

This loop prints each value in the dictionary. To iterate over both
keys and values, use the .items() method:

# Example Usage stable


for k, v in my_dict.items():
print(k, v)
This loop prints each key-value pair in the dictionary.
Nested dictionaries in Python are great for representing complex
relationships and hierarchies. Let’s explore how to use them.

Dictionary of Dictionaries
You can have a dictionary where each key’s value is another
dictionary. This helps you organize information neatly:

my_devices = {
'rtr1': {
'host': 'device1',
'device_type': 'cisco',
},
'rtr2': {
'host': 'device2',
'device_type': 'junos',
}
}

Here, each router (‘rtr1’ and ‘rtr2’) has its own dictionary with
details like ‘host’ and ‘device_type’.

Chaining Keys for Access


To access information in nested dictionaries, chain the keys
together:

# Accessing the device type of 'rtr1'


stable
device_type_rtr1 = my_devices['rtr1']['device_type']
This way, you can navigate through the nested structure.

Dictionary Containing Lists


Dictionaries can also have lists as values:

sf = {
'routers': ['192.168.1.1', '192.168.1.2'],
'switches': ['192.168.1.20', '192.168.1.21']
}

Here, ‘routers’ and ‘switches’ have lists of IP addresses.

Nesting Dictionary Inside a List


You can also nest dictionaries inside a list:

network_devices = [
{'device_type': 'router', 'ip': '192.168.1.1'},
{'device_type': 'switch', 'ip': '192.168.1.20'}
]

This list contains dictionaries, each representing a network device


with ‘device_type’ and ‘ip’ attributes.

Nested dictionaries in Python help you structure and organize


data efficiently, making it easier to model complex relationships.
Python dictionaries are versatile and essential for organizing data.
stable
Their dynamic features and support for nested structures make
them powerful tools in Python programming and network
engineering.

stable

You might also like