How to Filter Lists in Python
One of the very important things that Python offers to programmers, is the great lists handling functions. Lists are one great data type that you can utilize for lots of different tasks. Since Python is a high level language, it makes our handling lists a real breeze.
What is a Python List ?
A Python list is simply a data type containing values of the same type that can be changed dynamically. It can be initialized having just two elements and then we can go on adding more elements or delete some elements as well. On the other hand, the other important Python data structure, tuples, is the same as lists only immutable. Thus, it cannot be changed on runtime.
Why Do You Need to Filter Lists ?
What happens with standard Python programming is that we tend to create lists all the time. We could be either reading a file and creating a list with its words, or maybe getting a list of the files of our current directory. Whatever the case, lists are used quite a lot and if you have been programming in Python for just a small period of time, chances are that you have already used them at least once. The problem is that lists alone are not enough. Most times we need some time of filtering in order to throw out some elements and only keep the important ones. A good example could be the case of getting a list of files. Then, we just want to keep the ones that are of *.mp3 filetype. This is where Python starts to become really versatile, as you will see below.
How to Filter Lists in Python ?
The simplest way to filter a list is the one show below. We just have a list of filenames and we want to recreate that list so that it does not contain the files that do not have “mp3″ as their filetype.
list = ["1.mp3","2.txt", "3.mp3", "4.wmv","5.mp4" ] temp =  for item in list: if item.find(".mp3") != -1: temp.append(item) for item in temp: print item
In this example, we have a list that we traverse using a simple for. Then, we have another temporary list and whenever we locate an element having “.mp3″ as a substring, we add it to our new list. In the end we just print the new list and get the filenames we opted for.
Sometimes, we just need to quickly create a sublist of our starting list. Say that we just need the first two elements of our list. This can be done like this :
list = ["1.mp3","2.txt", "3.mp3", "4.wmv","5.mp4" ] list = list[:2] for item in list: print item
The syntax [starting:ending] determines the sublist we need to create. If no starting index is specified, it defaults to the first element. If you specify the ending index as n, you get the n-1 elements. So, li[:2] gets you elements li and li.
The Elegant Way to Filter Lists
def isMp3(s): if s.find(".mp3") == -1: return False else: return True list = ["1.mp3","2.txt", "3.mp3", "4.wmv","5.mp4" ] temp = filter(isMp3,list) for item in temp: print item
This is probably the best and most elegant way to filter a list in Python. We utilize the python function filter() here. What this does is take a function and our list to be filtered as its 2 parameters. For each element of the list, this function is executed and the parameter is the next element of the list. If this function returns true, the item is added to the resulting list, else it is not. Once finished, filter() returns the new list, which we print out as you see in the example above. If you want, you can also check the map() function as well.
This concludes this small post about filtering lists in Python. I reckon these techniques pretty important in writing efficient code. Python is mostly about doing things fast. Hence, using the built in functions can save you time (and money?) and also transform your code to an elegant state of the art (or at least make it bearable :P)