Category Archives: python

Python list comprehensions

I don’t know if you have used list comprehension in Python. It is a great feature. You can go through a list and do some small changes with very little effort. Here is an example;

For example you have a list of user names and you would like to shorten these user names to 8 characters.

As you know you can iterate a python list with a construct like;

If you want to do some operations on this returned value (user), you can just put an expression in front of this for loop and remove the colon like;

The output from the code above will be ;

It looks fine. What about the user names laving less than 8 characters. Assume you want to fetch those which have at least 8 characters or more and ignore the others! It is also possible with a one liner. If you put an if condition right next to our for loop, magic happens;

The new output will be like;

To be honest, although list comprehension is quite nice, to me it looks a bit perlish way. Because when you look at the statement it looks like a complex one liner but if you can break it down to pieces like “operation : for expression : if statement” it is really easy!

How to preserve quoted strings in python split

If you want to analyse for example apache log files and split the lines by space by using the usual “split” method, you will see that split doesn’t respect quoted strings. For example if you have a line like below; – – [06/Mar/2012:10:02:22 +0100] “GET /2011/10/19/jncip-sec-exam/ HTTP/1.1” 200 3331 “-” “mm”

You can’t get the HTTP_REQUEST easily with split. There is a very nice module named
shlex which allows you to split strings by space and treats quoted strings as single columns. Below is an example of my code which shows how you can fetch HTTP REQUEST from an apache log.

Finding the frequency of words in a file with python

If you want to find how many times a single word is repeated in a file, I have quoted my code below. The method that I didn’t know before was the “get” method. It allows you to get the value of the key but if it isn’t set before, set the value specified in our example 0. This allows us to count the occurrence indeed.