· python

Notes on Python: Lists

Like the numerable daily lists made with pen and paper, the list in Python is an incredibly handy way to store information, but with extra power.

We can start with an empty list:

>>> switches = []
>>> type(switches)
<type 'list'>

add to it:

>>> switches.append('switch-01')
>>> switches
>>> switches.append('switch-02')
>>> switches
['switch-01', 'switch-02']

Create another list, and concatenate these two lists into a new list:

>>> routers = ['router-03', 'router-01', 'router-02']
>>> routers
['router-03', 'router-01', 'router-02']
>>> devices = switches + routers
>>> devices
['switch-01', 'switch-02', 'router-03', 'router-01', 'router-02']

Loop through our new list:

>>>count = 0
>>>for device in devices:
...		count += 1
...     print 'Hostname:', device
Hostname: switch-01
Hostname: switch-02
Hostname: router-03
Hostname: router-01
Hostname: router-02
>>>print 'There are', count, 'devices.'
There are 5 devices.

Sort the list:

>>> devices
['switch-01', 'switch-02', 'router-03', 'router-01', 'router-02']
>>> devices.sort()
>>> devices
['router-01', 'router-02', 'router-03', 'switch-01', 'switch-02']

Delete elements from the list:

>>> devices.pop()
>>> devices
['router-01', 'router-02', 'router-03', 'switch-01']
>>> devices.remove('router-03')
>>> devices
['router-01', 'router-02', 'switch-01']

Find the number of items in our list, and the maximum and minimum elements in the list:

>>> len(devices)
>>> max(devices)
>>> min(devices)

Select elements in the list through their index (not forgetting that python lists are zero-indexed!):

>>> devices[0]
>>> devices[1]

Lists can be really useful as an interim holding bay while parsing a file, or extracting information from a directory, such as a folder of show run or show version files.

This brief summary really is brief and pulls heavily from others:

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket