LearnRevitAPI

#️⃣The art of writing clean python code with comprehensions.

Feb 21, 2025

How to Write Clean Code?

Writing clean and minimalist code is an Art.

There are many ways to reduce unnecessary fluff, but comprehensions are by far the simplest and most effective.

They’re like a secret superpower for Python: easy to learn, elegant to use, and incredibly useful for transforming your data in just one line.

Plus, they make your scripts easier to read—so you can spend more time on the fun stuff and less time debugging messy loops!

Let me share the secret of how to use them.

Get Ready, it's about to get hot!

What Are Comprehensions?

Comprehensions let you write shorter, cleaner, and faster code.

Whether you're dealing with lists, dictionaries, sets, or even variables - comprehensions will save you a lot time and effort.

Plus, they make for some pretty impressive one-liners!

If Comprehensions

Let's begin with simple if/else Statement comprehensions.

They let you combine simple logical statements into a single line. They are great to use when you need to check something and assign one of 2 available values.

I'm going to provide you 2 code examples with the same result, and I want you to tell me which one is easier to read.

Example #1:


Example #2:

I hope we can agree that Example #2 is more pythonic 🐍.

Also, it's very self-explanatory. You can literally read it as English and it makes sense.

Simple List Comprehensions

Now it's time to focus on the most used comprehensions in python - List Comprehensions.

They are a great way to transform, filter or create new lists in a more pythonic way. They allow you to create a loop and generate a new list of data from a single line of code.

Here is the List Comprehension Anatomy:

As you can see you can put your for-loop inside of square brackets, and the first expression is what will be added to the new list.

Let's have a look at 2 code examples again, and you tell me which one you prefer more.

Both examples will take items from an existing list and create a new list with UPPERCASE items (The same principle can be applied for anything else.)

Example #1:

Example #2:

Would you rather write Example #1 or Example #2?
👉I bet it's #2!

List Comprehensions with Logic

I hope you already see how powerful comprehensions can be in your scripts.

However, some of you probably start wondering: 'But what if you want to add logic with if-statements'? Well, that's also possible.

Here is an extended anatomy of list comprehensions:

As you can see, we can create a for-loop in the middle, then specify expression of what will be returned on the left, and then conditions on the right.

This way you will only get items in your new list if they pass the condition. Otherwise they will be ignored. This is a huge time saver to filter your lists.

I will use the same example and add a simple filtering to ensure it contains 'item_' keyword.

P.S.

If you found this useful and you want to dive deeper with python for Revit API, then you will love pyRevit Basics course.

It's made specifically for Revit Users, so I can relate python and Revit API concepts to something you already understand inside of Revit. Click here to learn more.

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 9500+ people!