If you are reading this post then you probably want to write beautiful, clean, readable code that has people’s mouths watering when they look at it. Well done, that’s the right mindset to have. If you work or are planning to work for a company, or maybe contribute to open source software there is something you should really keep in mind. Lots and in the case of open source, possibly thousands of other people have to read your code.
Depending on the current state of the code you write it could be a very large, or very small job. Whichever it is, it is definitely worth putting a little time and effort into. The best way to do it is step by step, little by little. Think of it like a loop and with each iteration, add a new change to the way you code. That’s what I’ve been doing and it’s working well. I can also look through my past code and visibly see my code becoming better and better.
Anyway, let’s get on with the second post in this series on common pythonic idioms and dive into lists.
HOW TO HANDLE LISTS, THE PYTHONIC WAY
Lists are a very commonly used data structure and have many uses. They are one of Python’s sequence types, which means they can be used with many of the built in Python methods that take them.
Again, when dealing with lists there are often more than one way of doing things, but there will often be a ‘pythonic’ way. These are usually faster and more readable than the others, so let’s make use of them.
CREATING A LIST INITIALISED WITH X VALUES
When creating a list you will often want to initialise it a with certain number of values.
#How not to create a list initialised with x values
new_list = 
for x in range(10):
new_list[x] = None
#How to create a list initialised with x values
new_list = [None] * 10
As you can see here Python has a very useful and compact way of doing this. By simply adding an asterisk and the amount of times you want the value to repeat, you can easily create a list containing that value x times.
List comprehensions are actually a fairly large topic and an extremely versatile tool to have in your toolbox. I definitely recommend reading over the Python documents for these.
#An example of a list comprehension
list_1 = range(1, 101)
list_comp = [val for val in list_1 if val % 2 == 0]
This example takes the variable list_1 which contains all integers from 1 to 100 and creates a new list containing only the numbers in list_1 that are divisible by 2. It can be used like so: [f(x) for x in sequence (conditional)] where f(x) is some function of x and (conditional) is an optional conditional statement. They can also be nested within one another.
JOINING A LIST INTO A STRING
Joining a list into one string is another task that you may find yourself having to do. In Python this is very easy, but at least in my opinion not very intuitive.
#How not to join a list into a string
strings = ["H", "e", "l", "l", "o"]
string = ''
for char in strings:
string = string + char
#How to join an list into a string
strings = ["H", "e", "l", "l", "o"]
string = ''.join(strings)
As shown in the above code, the pythonic way to join a list into a string is to call the join() function on an empty string, passing it the list that you want to combine. You can read more about this in the Python documentation.
Lists. You will use them a lot in your Python career, so it’s definitely worth committing these to memory. I found that when I started using these idioms, my code instantly became more readable and was much easier to work with. I hope you can get some value out of these just like I did.
This was the second in a series of posts on common pythonic idioms.