Python Lists

A python list is, as the name implies, a list of items. A list can be an alternative to using multiple variables.

Let’s say I want to use the IP addresses of different devices in my Python program. I could create a new variable for each and assign a string:

>>> R1_ip_address = "192.168.1.1"
>>> R2_ip_address = "192.168.1.2"
>>> R3_ip_address = "192.168.1.3"

It might be easier however to use a list. Here’s an example:

>>> router_ip_addresses = ["192.168.1.1","192.168.1.2","192.168.1.3"]
>>> switch_ip_addresses = ["172.16.1.1","172.16.1.2","172.16.1.3"]

Above, we have a list with strings but you can add almost everything in a list. For example, numbers:

>>> my_numbers = [1,2,3,4,5,6]

Even a combination of strings and numbers is possible:

>>> random_stuff = ["R1",5,"SW1",7]

Access items in list

I can access items in the list by setting the number:

>>> router_ip_addresses[0]
'192.168.1.1'

Python starts counting from 0, that’s the first item in the list. Let’s access the second item:

>>> router_ip_addresses[1]
'192.168.1.2'

I can also start from the last item in the list by supplying a negative number. For example:

>>> router_ip_addresses[-1]
'192.168.1.3'
>>> router_ip_addresses[-2]
'192.168.1.2'

Adding or removing items in list

We can also add or remove items from our list.

Add item

Let’s add something:

>>> router_ip_addresses.append("192.168.1.4")

And verify our work:

>>> router_ip_addresses
['192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4']

We successfully added another IP address to our list.

Remove item by number

Let’s remove the first item in our list:

>>> router_ip_addresses.pop(0)
'192.168.1.1'

The first item in our list is now gone:

>>> router_ip_addresses
['192.168.1.2', '192.168.1.3', '192.168.1.4']

Remove item by name

We can also remove an item by name. It works like this:

>>> router_ip_addresses.remove("192.168.1.3")

Let’s verify our work:

>>> router_ip_addresses
['192.168.1.2', '192.168.1.4']

Nested Lists

We have seen we can add strings and numbers to a list, but you can also add a list to a list. Right now, we have two lists with strings:

>>> router_ip_addresses
['192.168.1.2', '192.168.1.4']

>>> switch_ip_addresses
['172.16.1.1', '172.16.1.2', '172.16.1.3']

Let’s add these two lists to another list:

>>> routers_switches = [router_ip_addresses, switch_ip_addresses]

Here’s what our new list looks like:

>>> routers_switches
[['192.168.1.2', '192.168.1.4'], ['172.16.1.1', '172.16.1.2', '172.16.1.3']]

The list contains two lists.

Concatenate lists

It’s also possible to combine two lists into a new list. We have two separate lists:

>>> router_ip_addresses
['192.168.1.2', '192.168.1.4']

>>> switch_ip_addresses
['172.16.1.1', '172.16.1.2', '172.16.1.3']

Let’s combine these two into a new list:

>>> all_ip_addresses = router_ip_addresses + switch_ip_addresses

Here is our end result:

>>> all_ip_addresses
['192.168.1.2', '192.168.1.4', '172.16.1.1', '172.16.1.2', '172.16.1.3']

List Functions

There are some useful functions we can use on our lists.

Len

We can retrieve the number of items in the list with the len function:

>>> len(all_ip_addresses)
5

Min and Max

We can retrieve the highest or lowest value of a list:

>>> my_numbers
[1, 2, 3, 4, 5, 6]
>>> max(my_numbers)
6
>>> min(my_numbers)
1

Convert item into list

We can also convert something into a list with the list function. Here’s how to convert a string into a list:

>>> list("Router")
['R', 'o', 'u', 't', 'e', 'r']

We can also use the split function which can use a delimiter. This is useful when you want to convert a line in a CSV file to a list. For example:

>>> csv_line = "R1,192.168.1.1,IOS XE"

Let’s convert that CSV line into a list:

>>> R1_info = csv_line.split(",")

We specify the comma as the delimiter and we end up with a list:

>>> R1_info
['R1', '192.168.1.1', 'IOS XE']

Ask a question or start a discussion by visiting our Community Forum