5 Python Tricks You Should Know

How to supercharge your python beyond the basics with ease

Aaron S
Aaron S
Feb 21, 2020 · 6 min read

ython 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:

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]

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) # starts at 3
print(next(G)) # 3
print(next(G)) # 4
print(next(G)) # 5
print(next(G)) # 6

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'])


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])


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

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):

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.

For more tech/coding related content please sign up to my newsletter here.

Related Articles

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Aaron S

Written by

Aaron S

Hello! My name is Aaron. Healthcare professional with an interest in python, technology, education and healthcare.I run coding-medic.com for python enthusiasts.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Aaron S

Written by

Aaron S

Hello! My name is Aaron. Healthcare professional with an interest in python, technology, education and healthcare.I run coding-medic.com for python enthusiasts.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store