Although not the first language I picked up, I’ve really taken a liking to Python, especially recently. I’ve tried out many languages and Python is one of the languages that I find the most fun to program in. One of the reasons I enjoy programming in it so much is that Python code is neat, minimalistic and beautiful.
The syntax of the language is fantastic, but when you start changing the way you write code to be more ‘pythonic’, that’s when the real beauty kicks in. Pythonic code is code that makes use of the multitudes of ‘python idioms’ that exist, a python idiom being a specific way to code that is, well… pythonic.
This is the first in a series of posts on common pythonic idioms. Let’s kick it off with for loops.
HOW NOT TO WRITE FOR LOOPS (AND THEIR PYTHONIC COUNTERPARTS)
For loops are used a lot, and the following pythonic idioms for these common uses are often actually faster than their non-pythonic counterparts. Not to mention they are extremely readable.
Note that I refer to a ‘sequence’ a lot in this post. A sequence is a python type; a list, tuple or range object.
LOOPING OVER A RANGE OF VALUES
One common use of a for loop is to iterate over a range of values.
#This is how not to iterate over a range of values
for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
#This is how to iterate over a range of values
for i in range(11):
As you can see here the range() function is used. It loops up to but not including the value you pass it. You can provide just a stop value or a start, end and optionally step value too. For more information on the range function, take a look at the Python documentation.
LOOPING OVER A SEQUENCE, USING BOTH THE INDEXES AND VALUES
Another common use of a for loop is to loop over a sequence, doing things with its index and value pairs.
#This is not how to iterate over a list and use indexes and values
for i in range(len(array)):
#do stuff with i
#do stuff with array[i]
#This is how to iterate over a list and use indexes and values
for i, val in enumerate(array):
#do stuff with i
#do stuff with val
The enumerate() function returns a tuple of the current index and value pair for each iteration of the for loop which are unpacked into the two variables before it; in this case i and val. You can also pass an offset value to enumerate to start the index count from a number other than 0. For more information on the enumerate function, take a look at the Python documentation.
LOOPING OVER A SEQUENCE IN REVERSE ORDER
The final common use of a for loop that we will go over in this post is looping over a sequence in reverse order.
#This is not how to iterate over a list in reverse order
for val in range(len(array) - 1, -1, -1):
#This is how to itereate over a list in reverse order
for val in reversed(array)
The reversed() function simply iterates over a sequence backwards. There’s not really much more to know about it, but if you would like to read more about it, take a look at the Python documentation.
So there we have it, three common pythonic idioms for for loops. Hopefully you have learnt something from this as I did and gained the power to write more pythonic code.
This was the first in a series of posts on common pythonic idioms.