“The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code — not in reams of trivial code that bores the reader to death.”
- Guido van Rossum (creator of the Python programming language)
About this article
This article will cover 7 instances of code where you can convert your non-pythonic code into more legible and elegant code. Does your code have these usages ? Then, its a good time to revisit your old code and learn to write it in a more elegant manner.
The topics that we would be covering in this article are —
- changing data types in a data frame in bulk
- writing functions that have a conditional statement
- using list comprehensions instead of loops
- walrus operator
- using the lambda function for short functions
- when to use ‘
- using the
alloperators in lists
Use Case 1 :
When you have to change the type of many of the attributes of your pandas columns / attributes, use a dictionary to do it more elegantly
Case : You have a data-frame called df_box , which has 3 columns
And you want to change the data types of these columns into
The traditional way — change each of the columns explicitly line by line
df_vin = df_vin.astype(
Pythonic way — Pass the attributes / columns that you want to do type-casting as a dictionary .
df_vin = df_vin.astype(
Use Case 2 :
When writing a function that returns a value based on a condition, try writing in simple one line conditions for easy readability.
Traditional way -
def is_greater (a,b):
if a > b :
Pythonic way —
def is_greater(): return 1 if a > b else 0
Note : You can use the same way to write any ‘
Take Away : Most often writing a function in a single line (if that is all it takes) makes it more understandable and easier to handle. Lambda functions (see below) are an effective way to do that.
# Checks if m is an even number
is_even=1 if m%2 == 0 else 0
Use Case 3:
When you want to do any list operations, try using List comprehensions.
Case : Filter out even numbers from a list of numbers.
list1 = [4,2,3,5,7,8,11,16]
Traditional way — loop through the entire list and see if it is odd or even (using a conditional statement) and then append the odd ones into a variable that contains your desired output.
list1 = [4,2,3,5,7,8,11,16]list_odd = 
for n in list1 :
if n%2 ==1 :
Pythonic way — using list comprehension ( same operation in ONE line )
list_odd = [n for n in list1 if n%2 == 1 ]
As you see, python list comprehension is a very powerful way of doing list manipulations. For more understanding on list comprehension, please have a look at this article which gives you an intuitive understanding on list comprehensions.
Use Case 4:
Use the walrus operator when you want to assign the result of a condition-statement to a Boolean in a nifty manner.
Note : Available only from python 3.8 onward
:= is the assignment operator — (also known as the walrus operator ) since it resembles the face and the tusk of the walrus. Let’s see how this can be used to assign the output of a conditional statement into a variable.
Case : Check if the length of a list is greater than 3
my_list = [1,2,3,4,5]
Traditional way —
if len(my_list) > 3 :
n = True
Pythonic way — using the walrus operator
if n := len(my_list) > 3:
Displaying the output …
Use Case 5:
Use Lambda functions if you have a very small function definitions.
A lambda function is an anonymous function — for a good reason — it doesn't need/have a name.
lambda functions look like this in general :
lambda arguments: expression
Lets take a simple function to exhibit the use of lambda — ( though you might not be able to fully appreciate the power of lambda function ).
Case : print the square of all the numbers in a list
The traditional way of using a function —
Pythonic way — by using a lambda function
It has to be kept in mind that the best use of a lambda function when used along with
apply(), map() or the
Use Case 6:
To create a key value pair from two lists of the same length, use the ‘
Case : If you have two lists, one representing a list of numbers
and the other a list of letters corresponding to the numbers ,
[‘A’, ‘B’, ‘C’, ‘D’, ‘F’]
and you want to make a list of lists preserving the order of the lists —
The traditional way of doing it —
The Pythonic way —
Use Case 7:
To check ALL ( logical multiple AND ), ANY ( logical multiple OR ) condition iteratively in a list in a single line of code, use the all() or any() function
Case: If I want to do an OR / AND condition check on every element of a list.
Lets see some examples —
Example 1 — Check if any of the values in a given list
l_num is greater than 10 and Example 2 — check if all of the values is
less than 12
The traditional way —
The Pythonic way —
If you have enjoyed going through these samples and is getting started with Python, you might also be interested in this article —