Sorting in python

How to sort lists in python? The easiest option is to use the sort method.

For example, to sort the list of vendors you can use the following procedure:

>>> devices = ['cisco', 'juniper', 'alcatel']
>>> devices.sort()
>>> devices
['alcatel', 'cisco', 'juniper']

The python sort the elements alphabetically. Ok that was a simple example. What if we have a list of IP addresses, like in example bellow.

>>> ipAddresses = ['', '', '']
>>> ipAddresses.sort()
>>> ipAddresses
['', '', '']

This is not correct. The problem is that python sorts elements in list alphabetically. This means that first letter in the string '' is before the first letter in string ''. How to solve this?

The solution is to use the key argument in the sort method. The key argument expects the function, which will be called for each element and the result of the function will be used to sort elements in the list.

We can transform the IP address into decimal form and then sort these values based on the decimal value. The solution for sorting the IP addresses can be:

>>> import ipaddr
>>> def transformToDecimal(ip):
...  ipAddress = ipaddr.IPv4Network(ip)
...  return int(ipAddress)
>>> ipAddresses
['', '', '']
>>> ipAddresses.sort(key=transformToDecimal)
>>> ipAddresses
['', '', '']

Now the result is as we have expected. The sort function was calling the transformToDecimal function for every element in the list. The function returned the decimal representation of the IP address, which was then used for sorting the elements in the list.

Now continue with a little more complex problem. How to sort the list of dictionaries by the values. For example, if we have the following list of dictionaries:

deviceList = [{'vendor': 'cisco', 'ipAddress': ''}, {'vendor': 'juniper', 'ipAddress': ''}, {'vendor': 'alcatel', 'ipAddress': ''}]

How can we sort these dictionaries by the vendor. The solution could be to implement own function that return the vendor value. But the python has similiar function that is already built-in. The module is called operator. So the complete solution is:

>>> import operator
>>> getVendor = operator.itemgetter('vendor')
>>> deviceList
[{'ipAddress': '', 'vendor': 'cisco'}, {'ipAddress': '', 'vendor': 'juniper'}, {'ipAddress': '', 'vendor': 'alcatel'}]
>>> deviceList.sort(key=getVendor)
>>> deviceList
[{'ipAddress': '', 'vendor': 'alcatel'}, {'ipAddress': '', 'vendor': 'cisco'}, {'ipAddress': '', 'vendor': 'juniper'}]

The itemgetter method create a function that return dictionary value, of the key that is specified as an argument. In our example, we are using 'vendor' key.

So the sort option is very simple for the simple lists. But for the more complex lists you can specify your own sorting key, which could be very useful.

No comments:

Post a Comment