Python Tip #1: Intersection of two lists

This will be the first of what I’m going to try to be a daily series of small tips and tricks in Python.

We have two lists:

A = [1, 2, 3, 4, 5, 6]  and  B = [10, 9, 8, 7, 6, 5]

To get the elements that are in both lists:

def intersect(*lists):

    return reduce(set.intersection, lists)

The only trick is to actually pass the lists to the function not as lists but as sets. [EDIT: This can be done before the function call or just change the function to what Paddy wrote on the 1st comment] Bad part: if the lists contain duplicate elements, there they go. Sets only contain unique items. Which, by the way, leads to another question “What if I want to eliminate duplicates from a list?”:

newlist = set(oldlist)

Mass renaming of files

I don’t know if it is just my camera, but everytime I transfer my pictures to the laptop, I have a boring time whenever I have to either upload them anywhere, or just use them in a program that requires a “jpg” or “jpeg” extension. Why? Because my camera uses the “JPG”/”JPEG” format to save the pictures, which, in Linux, as the filenames are case-sensitive, is a pain the rear. Yes, I know, Linux sucks. But luckily, it also has its own “de-sucking” tools, which can be handy in this particular situations, but also in several other occasions, some that even Windows users might also complain about :P

Meet rename. Rename is a handy bash tool/command with the simple syntax: rename <regexp> <files>

It comes with 3 options: the ubiquitous -v (verbose), -f (force), and -n (no-act) which outputs what would have been changed.

The tough part might be the Regular Expression. I advise this link for those who don’t know anything about it.

And, to show some examples of how it works:

joao@wasp:~/Desktop$ ls | grep docx
Events.docx
Keep Management Guide.docx
Neovir Campaign Setting.docx
joao@wasp:~/Desktop$ rename -n 's/docx/doc/' *.docx
Events.docx renamed as Events.doc
Keep Management Guide.docx renamed as Keep Management Guide.doc
Neovir Campaign Setting.docx renamed as Neovir Campaign Setting.doc
joao@wasp:~/Desktop$ 

The regular expression ‘s/docx/doc/’ stands for ‘substitute/this/forthis/’. You can use wildcards (*) to match several variants of a same name for example, and remember that if you with to substitue a /, you have to escape it with .

I was using a for loop before I found this :x Quite a change!