Site icon Karneliuk

CEX (Code EXpress) 04. List what you need.

Hello my friend,

We continue discussion about the variable in the Python, which we have started in the previous article. Today we cover new type, which is very useful and, therefore, widely used. In various programming languages, this type is known under various names. So, who is here? 

Network automation training – boost your career

Don’t wait to be kicked out of IT business. Join our network automation training to secure your job in future. Come to NetDevOps side.

How does the training differ from this blog post series? Here you get the basics and learn some programming concepts in general, whereas in the training you get comprehensive set of knowledge with the detailed examples how to use Python for the network and IT automation. You need both.

What are we going to do today?

Answering to the question in the end of the preamble, here is the list. It is also known under the name array; however, in the Python’s terminology list is the correct term. The list variable contains any amount of the elements (starting from zero), which are siblings to each other, and they can be of any type as described in the previous article. So, we will take a look on:

  1. How to create a list variable
  2. How to call the elements or the list as a whole
  3. How to add the elements to the list using specific functions
  4. How to remove the elements from the list using specific functions

Why does it matter?

The lists are truly widely used in various fields of the programming, and the network automation field has a number of great use cases illustrating that. For instance, you need to run the set of tasks to multiple network functions:

Digging further in the mentioned use cases, you will find the there is a room for lists there as well. For instance:

How are we doing that?

Following the approach, we have started earlier (link to the 2nd article), we create a new directory in our GitHub lessons:


1
2
3
$ cd CEX
$ mkdir 04 && cd 04
$

Inside the directory we create a new file called lists.py, which we will use during this class:


1
2
3
$ touch lists.py && chmod a+x lists.py
$ cat lists.py
#!/usr/local/bin/python3.8

If you have a question to chmod command or to the path to the Python interpreter, take a look on the corresponding class.

There are two ways how you can create a list variable:

Let’s start with the second way as it allows us to cover some important concepts working with the elements, before we touch the list-oriented functions.

#1.  Creating the list variable with the elements

The list variable is created using the var_name = [element0, element1, …, elementN] syntax. Each element has an index starting from 0, which could be used to call the value of this particular element. This is achieved using the construction var_name[index]. On the other hand, if you need to get all the elements from the list, you just call var_name without the index. Take a look on the example below:


1
2
3
4
5
6
7
8
9
10
$ cat lists.py
#!/usr/local/bin/python3.8

network_functions = ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']

print('All the network functions in the network: {}'.format(network_functions))

print('The following devices are the service provider routers: {}, {}'.format(network_functions[0], network_functions[1]))
print('The following devices are the data centre switches: {}, {}'.format(network_functions[2], network_functions[3]))
print('The following devices are the enterprise campus switches: {}, {}'.format(network_functions[4], network_functions[5]))$

The naming conventions for the list variables follow the same rules as other variables.

To verify the value of the certain elements and create a meaningful report you can use the print() and format() together, as you have seen in the previous class. Let’s execute this Python script to check the result of its execution:


1
2
3
4
5
$ ./lists.py
All the network functions in the network: ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']
The following devices are the service provider routers: PE1-BLN-DE, PE2-BLN-DE
The following devices are the data centre switches: de-bln-eq1-leaf101, de-bln-eq1-leaf102
The following devices are the enterprise campus switches: sw1-s1-hq-bln, sw2-s1-hq-bln

Inside the list you can also mix any data types you wish, if that makes sense for you. Take a look on the list router:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ cat lists.py
#!/usr/local/bin/python3.8

network_functions = ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']

print('All the network functions in the network: {}'.format(network_functions))

print('The following devices are the service provider routers: {}, {}'.format(network_functions[0], network_functions[1]))
print('The following devices are the data centre switches: {}, {}'.format(network_functions[2], network_functions[3]))
print('The following devices are the enterprise campus switches: {}, {}'.format(network_functions[4], network_functions[5]))


router = ['PE1_bln_de', 12, True]

print('Router parameters: {}'.format(router))
print('Hostname: {}'.format(router[0]))
print('Number of 100G uplinks: {}'.format(router[1]))
print('In operation: {}'.format(router[2]))

Despite this typically is not the best application of the list, it works well. And here is the execution of this Python code:


1
2
3
4
5
6
7
8
9
$ ./lists.py
All the network functions in the network: ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']
The following devices are the service provider routers: PE1-BLN-DE, PE2-BLN-DE
The following devices are the data centre switches: de-bln-eq1-leaf101, de-bln-eq1-leaf102
The following devices are the enterprise campus switches: sw1-s1-hq-bln, sw2-s1-hq-bln
Router parameters: ['PE1_bln_de', 12, True]
Hostname: PE1_bln_de
Number of 100G uplinks: 12
In operation: True

Some classes later you will learn the Python dictionary, which is more suitable for such a use case.

#2.  Creating the empty list and adding variables

There is another possibility to create a working list variable: you can create an empty list and then add the variables. The empty list is create using the syntax var_name = [] without any elements inside. Then we can add any element using append() or expand() function. Let’s take a look on such an example:


1
2
3
4
5
6
7
8
9
10
$ cat lists.py
#!/usr/local/bin/python3.8

! SOME OUTPUT IS TRUNCATED FOR BREVITY

network_functions_new = []
print('Current network functions: {}'.format(network_functions_new))

network_functions_new.append('XR1')
print('Current network functions: {}'.format(network_functions_new))

In this snippet we create an empty list’s variable network_functions_new and using the print() and format() function we verify its content. In a moment you will see that there is no meaningful output, as there are no elements inside. Afterwards, using the function append() we add an element to the list network_functions_new and then we print the content once more. The following example shows these details:


1
2
3
$ ./lists.py
Current network functions: []
Current network functions: ['XR1']

If you want to add another list to an existing one, you should use a function extend() as demonstrated in this example:


1
2
3
4
5
6
7
8
9
10
$ $ cat lists.py
#!/usr/local/bin/python3.8

! SOME OUTPUT IS TRUNCATED FOR BREVITY

network_functions_new = []
print('Current network functions: {}'.format(network_functions_new))

network_functions_new.extend(['XR1'])
print('Current network functions: {}'.format(network_functions_new))

And here is the execution of this Python script:


1
2
3
$ ./lists.py
Current network functions: []
Current network functions: ['XR1']

Point out, you should use extend() when you add a list to another list, and append() when you add an element.

Instead of putting the exact values of the list or variables to an input of a certain function, you can put the names of the variables there. It is entirely up to you, how you make your list bigger.

#3.  Removing elements from the list

There are two major approaches, how you can remove the element from the list: using the index or the variable’s value. In the first case you should you use the pop(index) function, where index is contains the index of the variable. You can also assign a value to to another variable using this function:


1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat lists.py
#!/usr/local/bin/python3.8

network_functions = ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']

print('All the network functions in the network: {}'.format(network_functions))

print('The following devices are the service provider routers: {}, {}'.format(network_functions[0], network_functions[1]))
print('The following devices are the data centre switches: {}, {}'.format(network_functions[2], network_functions[3]))
print('The following devices are the enterprise campus switches: {}, {}'.format(network_functions[4], network_functions[5]))

value = network_functions.pop(0)
print('We have removed: {} and left: {}'.format(value, network_functions))

And this is how it works:


1
2
3
4
5
6
$ ./lists.py
All the network functions in the network: ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']
The following devices are the service provider routers: PE1-BLN-DE, PE2-BLN-DE
The following devices are the data centre switches: de-bln-eq1-leaf101, de-bln-eq1-leaf102
The following devices are the enterprise campus switches: sw1-s1-hq-bln, sw2-s1-hq-bln
We have removed: PE1-BLN-DE and left: ['PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']

In the closing part of the Python’s 3.8 script you see that the function pop() is applied to the list network_functions. And, using the print() function you can see both the value removed from the list and what remains there.

The second option to delete the elements from the list is to use the function remove(). The input to the function is the value of the any list’s element. The first element, which has a full match is removed. Here is the example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ cat lists.py
#!/usr/local/bin/python3.8

network_functions = ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']

print('All the network functions in the network: {}'.format(network_functions))

print('The following devices are the service provider routers: {}, {}'.format(network_functions[0], network_functions[1]))
print('The following devices are the data centre switches: {}, {}'.format(network_functions[2], network_functions[3]))
print('The following devices are the enterprise campus switches: {}, {}'.format(network_functions[4], network_functions[5]))

value = network_functions.pop(0)
print('We have removed: {} and left: {}'.format(value, network_functions))

value1 = network_functions.remove('de-bln-eq1-leaf101')
print('We have removed: {} and left: {}'.format(value1, network_functions))

The execution of this scripts looks slightly different to the previous script:


1
2
3
4
5
6
7
$ ./lists.py
All the network functions in the network: ['PE1-BLN-DE', 'PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']
The following devices are the service provider routers: PE1-BLN-DE, PE2-BLN-DE
The following devices are the data centre switches: de-bln-eq1-leaf101, de-bln-eq1-leaf102
The following devices are the enterprise campus switches: sw1-s1-hq-bln, sw2-s1-hq-bln
We have removed: PE1-BLN-DE and left: ['PE2-BLN-DE', 'de-bln-eq1-leaf101', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']
We have removed: None and left: ['PE2-BLN-DE', 'de-bln-eq1-leaf102', 'sw1-s1-hq-bln', 'sw2-s1-hq-bln']

As you can see from the associated example, the remove() function doesn’t return any value, whereas pop() does. However, both deletes the elements from the list. Each in its own approach.

If you prefer video

If you prefer watching the video instead of reading blogpost, that is also wonderful. We value your time and want to create for you the best experience possible. Therefore, for all the blogposts associated with CEX series, we will record the video and post it on our YouTube channel.

And here is the video for this blogpost.

What else shall you try?

Programming is all around testing, trying, breaking and fixing the things. Try to do the following things to collect some more experience:

Lessons in GitHub

You can find the final working versions of the files from this blog at our GitHub page.

Conclusion

List is one of the most popular types of the variables. Soon you will see, that it is so popular, that it is hard to imagine the Python’s 3.8 (or any other) program without that. In the next blogpost out of this series you will learn how to convert list to variables and back using some functions. Are you thrilled in Python like we? Subscribe in the YouTube and follow us. Take care and goodbye!

Support us






P.S.

If you have further questions or you need help with your networks, I’m happy to assist you, just send me message. Also don’t forget to share the article on your social media, if you like it.

BR,

Anton Karneliuk 

Exit mobile version