Three Python Tips to Improve Your Workflow

Aadit Ambadkar
Geek Culture
Published in
4 min readJun 25, 2021

--

Photo by Chris Ried on Unsplash

Here are three of my favorite Python “life-hacks” which helped me improve my workflow.

Ready?

1. Using List Comprehension

Not a lot of people fully understand list comprehension, and hence they don’t use it to its fullest potential. However, when used properly, list comprehension can be a really powerful tool.

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. (W3 Schools)

Their syntax is as follows:

[expression for item in list if condition]

Let’s take a look at an example:

Say you had a list of lists, containing different foods and their respective sweetness levels. If you wanted to get a list of foods with a sweetness level above 3, for example, you could use the following code:

Don’t just take my word for it, try it out! Here is the code if you want to copy-paste it:

list_of_foods = [[“Apples”, 3],[“Bananas”, 5], [“Rice”, 2], [“Ice Cream”, 10]]
list_of_filtered_foods = [x for x in list_of_foods if x[1]>3]
print(list_of_filtered_foods)

With proper list comprehension, you can easily increase efficiency and boost your workflow.

List comprehension also works with Dictionaries.

2. Import Effectively

This tip is really 3 different tips combined

2.1 — Reminder Service

We have all had the experience of running a python file and realizing you are missing so many imports because you thought you imported them, but you didn’t. Most of the time, a good IDE will have auto imports. But sometimes, even the best IDEs can forget to import things as well.

Well, worry no more because there is a quick fix to this.

Simply add a comment the moment you start coding to reminding yourself to import the needed packages. A message like so would work just fine:

# Did you import?

Make sure that the comment is always at the bottom of your code. That way, as you are coding, you always see a reminder sticking out, reminding you to import!

2.2 — Using multiple lines

We all tend to import many things on the same line, especially if they are related.

That is the wrong habit however. Even though it is painfuly tedious to create a new line for each import, it helps you to stop and think if you really need that import, and if you need any others.

Using multiple lines also allows you to easily identify which imports are throwing errors. For example, take a look at the following code:

import numpy, pd

The interpreter would throw an error letting you know that numpy was missing. But, an IDE might not tell what is wrong. And, if all of your imports are on the same line, then good luck even finding where numpy is, let alone remove it.

Using multiple lines is extremely useful for IPYNB python. Again, if all of your imports are in the same code block, then the entire thing may fail. This isn’t to say 1 code block per import, but just don’t cram everything in one code block.

2.3 — From ___ Import *

Picture from Nerd Lettering

The from ___ import * command can be etremely useful at times — but very dangerous at others. That’s why its best to consider using these sporadically, but not frequently.

If you are importing multiple different classes or functions from the same package or class, it probably would be best to just import everything.

But, don’t go overboard with it, or you might get some weird errors caused by two imported classes or functions having the same names.

This advice goes hand in hand with using the import ___ as ___ command, which can be used to import functions or classes with different names.

3. Ternary Operators — and their workarounds

In Python 2.4, ternary operators were introduced.

Don’t immediately open an idle and test it out, because it’s probably not what you think it is.

In true pythonic fashion, ternary operators were introduced as such:

value if condition else value

Here is some sample code, try it out:

However, this is really wordy and just unnecessary. Instead, we can do the following to simplify it:

(value_if_false, value_if_true)[condition]

This code is much simpler (but only 4 characters shorter) and uses the fact that 1 is truthy, and 0 is falsey. However, one thing to notice is that the true and false values are switched.

Here is some sample code:

Conclusion

Using List Comprehension, Importing Effectively, and Utilizing Ternary Operators. Those were the 3 main tips I had for boosting your workflow with Python. If you implement these tricks, I am sure you will find yourself spending less time scratching your head, and more time enjoying the code.

If you did enjoy reading this article and want more tips, tricks, or tutorials, make sure to follow me, and give this article a clap while you are at it.

--

--

Aadit Ambadkar
Geek Culture

'24 Student at Redmond High School. Python and C++ programmer. Teen AI Researcher. Mathlete. https://www.youtube.com/@codenatix