TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
✓
Angular
0%
✓
Astro
0%
✓
Svelte
0%
✓
Vue.js
0%
✓
Other
0%
✓
I only use React
0%
✓
I don't use JavaScript
0%
Programming Languages / Python / Software Development

Tips and Tricks for Clean, Readable Python Code

From decorators to list comprehensions, these built-in Python features help developers transform clunky code into clean, readable solutions without reinventing the wheel.
Nov 7th, 2024 10:00am by
Featued image for: Tips and Tricks for Clean, Readable Python Code

Learning to code comes is a steep climb. One side of the learning covers learning how to code and the other side includes how to do that elegantly. For me, that was the hardest part. I could brute force and clunkily solve many things but when it came to writing an elegant solution, no thank you, I’ll take the nested loops every single time. But of course, that doesn’t work for a few reasons since code should be DRY (Don’t Repeat Yourself), memory-friendly, and readable by other people. But Google was my friend, and I slowly came across tools that helped me put more elegant solutions together in simpler ways without reinventing the wheel. Here are some of Python’s built-in helpers that aid in code readability and ease.

*args and **kwargs

*args and **kwargs help make functions widely useable. Using *args as a functions parameter allows the function to take any arbitrary number of arguments. Without *args, integer and string arguments would have to be accounted for.

With *args:

And without it just throws an error…

**kwargs carries similar functionality as *args but does so for key value pairs. **kwargs can be used in a function that doesn’t have a required number or has an unknown number of key value pairs.

List Comprehension

List comprehensions allow developers to create lists using only a single line. Without using a list comprehension, you could build a numbers list using the following code:

A list comprehension turns that code into a single line. The basic syntax is:

[expression for item in iterable] and in simple code, it looks like this:

List comprehensions can also include filtering functionality.

Lists aren’t the only data structure you can create in this way. We can create a dictionary and set it using the same creation pattern. The dictionary’s basic syntax is:

{key_expression: value_expression for item in iterable}.

We can multiply each number in our numbers list by 10 to a by_tens dictionary.

We can also do this with a set. The basic syntax is

{expression for item in iterable}. The code looks like this:

zip()

zip() allows you to iterate over multiple lists simultaneously and create tuples of corresponding elements. zip() shortens what could have been a multi-line iteration into a single line of code.

zip() iterates over the length of the smallest list. If the given lists are of different lengths, the tuples will only be as long as the smallest list.  The code below provides a clear illustration of both above points:

Merging Dictionaries

You can merge dictionaries using the update() functionality or the dictionary unpacking syntax (**).

or you could use the update function to add the water_pets object to the land_pets object: land_pets.update(water_pets).