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 = ""
>>> R2_ip_address = ""
>>> R3_ip_address = ""

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

>>> router_ip_addresses = ["","",""]
>>> switch_ip_addresses = ["","",""]

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]

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

>>> router_ip_addresses[1]

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

>>> router_ip_addresses[-1]
>>> router_ip_addresses[-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("")

And verify our work:

>>> router_ip_addresses
['', '', '', '']

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)

The first item in our list is now gone:

>>> router_ip_addresses
['', '', '']

Remove item by name

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

>>> router_ip_addresses.remove("")

Let’s verify our work:

>>> router_ip_addresses
['', '']

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
['', '']

>>> switch_ip_addresses
['', '', '']

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
[['', ''], ['', '', '']]

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
['', '']

>>> switch_ip_addresses
['', '', '']

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
['', '', '', '', '']

List Functions

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


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

>>> len(all_ip_addresses)

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)
>>> min(my_numbers)

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,,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', '', 'IOS XE']

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