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[0] and li[1].
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)
If we can assume that none of the filenames we want to keep contain
.mp3
somewhere other than the end of the name, a more elegant way of doing it is using list comprehensionstemp = [i for i in list if “.mp3” not in i]
Nice one Jonathan, indeed there are more precise ways to do some things, like the one you suggest here.
Bonjour Fred,Ludo n’a rien à voir avec cet article, il ne fait que cor0mntem̷e; L’éditeur c’est moi Le lien est changé, rendons à César ce qui appartient à César.Continuez avec GreenIT, bon boulot.
Pythonist is like:
list = [“1.mp3″,”2.txt”, “3.mp3”, “4.wmv”,”5.mp4″ ]
temp = filter(lambda x: “.mp3” in x, list)
for item in temp:
print(item)
…“If we can assume that none of the filenames we want to keep contain .mp3 somewhere other than the end”
Why‽ Just use (not) i.endswith(‘.mp3’) instead of “.mp3” (not) in i
Calling a variable ‘list’ is not the best coding practice.. it works but ‘list’ is the name for the list type:
$ python -c ‘print list’
@Dan : You’re absolutely right, I totally missed that.
Why not just a list comprehension:
>> l = [“this.mp3”, “that.wav”, “theotherthing”]
>> [x for x in l if x[-4:].lower() == “.mp3”]
[‘this.mp3’]
Hi there Your current website runs up incredibly slow if you ask me,
I’m not sure who’s issue is that but facebook starts quite immediate.
On the other hand thanks for putting up terrific article.
I do believe it has been literally helpful visitor who
actually visit here. This is without a doubt brilliant
what you have concluded and wish to see more articles by you.
To obtain more knowledge through content which you post, I’ve added
this web site.