You are on page 1of 8

5 Python Tricks You Should Know

How to supercharge your python beyond the basics with ease


Aaron S

Python has such an amazing support network it can almost seem like there is too much to take in
all at once. This is normal and should be recognised. It can be confusing for those starting out.
You can find vast quantities of resources to grapple with the basics but then what ? Where do
you go to keep improving?

Here, I list my top 5 useful snippets of code which I felt pushed my level up further and made my
code better. You will start to see how you can implement these five things into your code.

1. List comprehensions
You may have heard of these before. A pattern of coding in python is so common that it warrants
a modification. Any time you want to use a for loop to create a list. This is where list
comprehensions can be useful.

They are readable and concise. They do take a bit of time to wrap your head around what is going
on. Let's dive in and see if we can figure this out.

We know list comprehensions should be thought of when you want to create a list. A list
comprehension begins with a set of square brackets. Let's get the syntax out of the way and we
can show how the two are alike

list_comp = [expression for item in items]

We want to replace a for loop that adds items to a list.

new_list = []
for item in old_list:
new_list.append(item**2)

Notes
1. A list called new_list is called
2. A for loop iterates over item in old list
3. We use the append method to add item**2 to the new list

So far simple.

A list comprehension would look like this

new_list = [item**2 for item in old_list]


Notes
1. We take the expression item**2 as the expression we want to add to a list, this comes first in
the list comprehension.
2. The for loop afterwards is the loop we want to iterate over
3. Enclosing this in square brackets and assigning it the variable new_list means that the
expression item**2 is added to new_listfor each item that is looped over.

Thereʼs no append method or: need and this fits on one line! We can even add in conditionals, to
select what gets added. This functionality also extends to sets and dictionaries! You can even
write list comprehensions with nested for loops!

A word of caution, these can be overused! Only use a list comprehension if itʼs a simple for loop
and you explicitly want to create a list.

Priscilla Du Preez from Unsplashed

2. Generators Expressions — save memory usage


When we have a large data set that we want to iterate over or get results from, but canʼt store all
that in memory. Think of generator functions. They are much like a function except instead of
return you switch that for the keyword yield.

They create iterators that need to be forced to spit out the values it holds. They donʼt store
objects in memory like a list or a set. They only give you one item at a time too. This is called lazy
looping. So when you want to read a large file in use a generator.
def gen(n):
while True:
yield n
n += 1 G = gen(3)
print(next(G)) # 3
print(next(G)) # 4
print(next(G)) # 5
print(next(G)) # 6

Notes
1. We create a function gen
2. Yield keyword stores the value n, holds onto it till we call the next() method
3. Using the assignment operator we add to 1 to n
4. This is an infinite loop and keeps going
5. We call the next() method, and it will continue to spit out values

Now, these values arenʼt stored, and only give to us when we call the next() method. A generator
creates an iterator, which relies on the next() method then to spit out values.

This is can be useful when we have a large data set and we want to stream in data without
overloading the memory. Please see here for further details.

Victoria Priessnitz from Unsplashed

3. Iterating over two objects with zip


You often find yourself with multiple objects which you want to iterate over, to collect data
together from each one. Zip is the function that allows you to do this! If you need to iterate say
over file names and its corresponding links. You can do this.

for file_name, link in zip(names, links):


print(file_name, link)

This steps through both objects at the same time, a tuple is returned with corresponding items
from each one. In the loop, we unpacked the tuple into separate values file_name and link.
zip() can take as many collections as you like, but will stop when the shortest one is exhausted.

Zip functions can be used to iterate over pairs of elements in the same object using a list
comprehension we spoke about.

For example:

differences = [next_el - elt for el, next_el in zip(items, items[1:])

For more information please see my articles on the zip function here.
Jake Nackos from Unsplashed

4. Counter — generate counts for objects


Counter is a dictionary subclass where the elements of an object are the keys and counts of the
items in the object are the values. Itʼs useful for when you need to count the number of objects.
To access counter, you need to import the builtin collections module.

Say you want to count all the occurrences of a string in a list.

import collections
counts = Counter(['Fred', 'Samantha', 'Jean-Claude', 'Samantha'])
print(counts)

Output:
Counter({'Samantha': 2, 'Fred': 1, 'Jean-Claude': 1})

What is great about the counter class is you can update it and the values can be accessed using
the dictionary API.

from collections import Counter


c = Counter('abcdaab')for letter in 'abcde':
print(letter,':', c[letter])

Output:

a : 3
b : 2
c : 1
d : 1
e : 0

Notes
1. c assigned to the Counter subclass with string ‘abcdaabʼ. The counter class provides individual
counts of each character. Each one can be accessed like a dictionary by c[item].
2. A for loop used to iterate over string ‘abcdeʼ, and assigning letter to each string characters.

3. Prints the variable letter and like a dictionary by inputting the string character. The count for
each letter is accessed by c[letter].

More objects can be inputted by the update() met, but for this and more please see here for
further details!
Eternal Seconds from Unsplashed

5. Chaining over multiple collections


If you need to iterate over multiple collections one at a time, the chain method from the itertools
module is a great way to do this.

for name in itertools.chain(first_name_list, second_name_list):


create_person(name)
This iterates through the first collection until it is exhausted, then moves on to the next and so on.
There are lots more interesting and advanced functions in the itertools module for more
information please see here

Hope you enjoyed this article.

About the author


I am a medical doctor who has a keen interest in teaching, python, technology and healthcare. I
am based in the UK, I teach online clinical education as well as running the websites
www.coding-medics.com.

You can contact me on asmith53@ed.ac.uk or on twitter here, all comments and


recommendations welcome! If you want to chat about any projects or to collaborate that would
be great.

Related Articles

Using the Zip function in Python Part 1


Python is an intuitive language and there are lots of resources to get you
through the basics of the language. Once the…

Everything you need to know about Enumerate()


Use pythonʼs enumerate function to change the way you loop forever

How to download files using Python


Understanding how to use Python to download files in your web
scraping projects

You might also like