Is this pythonic code?

Francisco Barcena - August 13, 2018

I love the Python programming language because of its standard data types: Numbers, Strings, Lists, Tuples, and Dictionaries. They allow your imagination to just run wild in how you implement a solution. If you dig deeper, your code will begin to converge toward the effective style that makes up coding the "pythonic" way. Pythonic code is code that is written in the most explicit and effective way.

To write things the pythonic way, you will need to make use of Python's iterative tools and functions, like groupby, Map, Filter, and Reduce, which will result in less lines of code. Your code will end up looking nicer with less indentation levels. You will feel dumb when you realize you never really needed those for loops inside those other for loops, which were inside that one big for loop.

In the image of this blog post you can see some old code that I wrote, it has many levels of indentation (yuck), but on the right, there are only 2! Yes, the code on the right looks like it has more lines, but this is because I'm using python's line continuation character "\" so that my code is more readable when horizontal space is limited, like when having many windows side to side. Is this use of filter functions an example of pythonic code? Yes, to an extent it is because the new code is shorter, more effective, and easier to read, you can clearly see that I'm filtering the "entries"