Skip to content

Lab 05 - Working with Lists

This lab explores basic list operations and applies them to commands for network devices. Sorting a list is also covered.

Task 1 - Explore Basic List Operations

In this lab, you'll start to work with lists and explore their built-in methods.

Step 1-1

Create the following list of MAC addresses and assign them to the variable mac_list:

>>> mac_list = ['00.00.00.00.11.11', '00.00.00.00.22.22', '33.00.00.00.33.33', '44:00:00:00:44:44']

Step 1-2

Print the value of mac_list:

>>> print(mac_list)
['00.00.00.00.11.11', '00.00.00.00.22.22', '33.00.00.00.33.33', '44:00:00:00:44:44']
>>>

Step 1-3

Using the replace method, update the fourth element in the list so that it uses periods instead of colons:

>>> mac_list[3] = mac_list[3].replace(':', '.')

This is to show you that you can update (over-write) a single element in a list while combining it with a concept you learned when working with strings.

Step 1-4

Print the new list.

Note: it's always a good idea to print after each change to see how the object was modified, especially when you're just getting started.

>>> print(mac_list)
['00.00.00.00.11.11', '00.00.00.00.22.22', '33.00.00.00.33.33', '44.00.00.00.44.44']
>>>

Step 1-5

Remove the last element using the pop built-in method.

Remember that pop, by default, removes and returns the last element in the list.

>>> mac_list.pop()
'44.00.00.00.44.44'
>>>
>>> mac_list
['00.00.00.00.11.11', '00.00.00.00.22.22', '33.00.00.00.33.33']
>>>

Step 1-6

Pop the MAC address '00.00.00.00.22.22'. Since this is NOT the last element, you must supply the index value of the value you're looking to pop.

>>> mac_list.pop(1)
'00.00.00.00.22.22'
>>>
>>> mac_list
['00.00.00.00.11.11', '33.00.00.00.33.33']
>>>

You needed to use the index value of 1 because "00.00.00.00.22.22" was the second element in the list and the value that has the index of 1.

Step 1-7

Insert '00.00.00.00.22.22' back into the list at the same position where it was. Use the insert method:

>>> mac_list.insert(1, '00.00.00.00.22.22')
>>>
>>> mac_list
['00.00.00.00.11.11', '00.00.00.00.22.22', '33.00.00.00.33.33']
>>>

Step 1-8

Insert the mac address '22.22.00.00.00.22' as the 3rd element:

The list we want is: ['00.00.00.00.11.11', '00.00.00.00.22.22', '22.22.00.00.00.22, '33.00.00.00.33.33']

>>> mac_list.insert(2, '22.22.00.00.00.22')
>>>
>>> mac_list
['00.00.00.00.11.11', '00.00.00.00.22.22', '22.22.00.00.00.22', '33.00.00.00.33.33']

Step 1-9

Add two more MAC addresses to the list in sequential order using the append method.

The MAC addresses to add are: 55.55.55.55.55.55 and 66.66.66.66.66.66.

>>> mac_list.append('55.55.55.55.55.55')
>>>
>>> mac_list.append('66.66.66.66.66.66')
>>>
>>> print(mac_list)
['00.00.00.00.11.11', '00.00.00.00.22.22', '22.22.00.00.00.22', '33.00.00.00.33.33', '55.55.55.55.55.55', '66.66.66.66.66.66']
>>>

Task 2 - Build a List of Commands for Network Devices

When working with APIs such as Cisco NX-API, commands are sent as strings to the device (for certain encoding types). However, while you are writing code, it's common to want to build the command set as a list. Lists are simply easier to work with and manipulate in terms of adding/removing certain elements/commands etc.

Step 2-1

Create a list of commands like the following:

>>> commands = ['interface Eth1/1', 'description configured by Python', 'shutdown']
>>>

Step 2-2

Convert the list of commands to a string and insert a semi-colon inbetween each command. Use the join method.

>>> cmd_string = ' ; '.join(commands)
>>>

Step 2-3

Print the new variable called cmd_string.

>>> print(cmd_string)
interface Eth1/1 ; description configured by Python ; shutdown
>>>

Take note what happened here. You can use join to insert any character(s) inbetween elements in a list yielding a string.

Step 2-4

Instead of inserting ";", now insert a "\n":

>>> cmd_string_n = '\n'.join(commands)
>>>

This inserted a new line in between each command.

Step 2-5

Print the new variable called cmd_string_n:

>>> print(cmd_string_n)
interface Eth1/1
description configured by Python
shutdown
>>>

Step 2-6

Perform the same two steps, but this time add a space after the "\n":

>>> cmd_string_n = '\n '.join(commands)
>>>
>>> print(cmd_string_n)
interface Eth1/1
 description configured by Python
 shutdown
>>>

Notice the subtle difference?

Step 2-7

Continue to try the other built-in methods for lists.

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>

Remember, you can use:

  • type() - check the data type of a variable
  • dir() - view built-in methods, e.g. dir(list) or dir(commands)
  • help() - view the help on how to use a given method, e.g. help(list.append) or help(commands.append)

Task 3 - Sort Lists of Similar Objects

Step 3-1

Create a list of Cisco Nexus linecards, noting they are all strings:

>>> n7k_linecards = ['N7K-SUP1', 'N7K-M132XP-12', 'N7K-M148GS-11', 'N7K-M148GT-11', 'N7K-F132XP-15', 'N7K-SUP1', 'N7K-M132XP-12', 'N7K-M132XP-12', 'N7K-M148GT-11','N7K-M148GT-11']
>>>

Step 3-2

Verify how many linecards there are either SUP2, SUP1, or M1-32 blades:

>>> n7k_linecards.count("N7K-SUP2")
0
>>>
>>> n7k_linecards.count("N7K-SUP1")
2
>>>
>>> n7k_linecards.count("N7K-M132XP-12")
3
>>>

Step 3-3

You can do the same for verifying how many device types of a given vendor are in your environment:

>>> vendors = ["cisco", "cisco", "juniper", "cisco", "arista", "juniper"]
>>>
>>> vendors.count('cisco')
3
>>>

Step 3-4

Sort the vendors list:

>>> vendors.sort()
>>>
>>> vendors
['arista', 'cisco', 'cisco', 'cisco', 'juniper', 'juniper']
>>>

Step 3-5

Let's now reverse the list using the optional keyword reverse:

>>> vendors.sort(reverse=True)
>>>
>>> vendors
['juniper', 'juniper', 'cisco', 'cisco', 'cisco', 'arista']
>>>

Note: Remember to use help(vendors.sort)!