15
Python 3.9 Features for Noobs
In this post, I will discuss some of the interesting features added in python 3.9!
Prior to python 3.9, there were mainly two ways to merge or combine two dictionaries
in python,
dict1 = {"name": "Salman", "age": "NA"}
dict2 = {"crimes": "lol", "career_destroyed": "uncountable"}
# Way1 : using update method
dict1.update(dict2)
print("dict1 =>", dict1)
# Way2: using ugly ** unpacking
new_dict = {**dict1, **dict2}
print("new_dict=>", new_dict)
"""OUPUT
dict1 => {'name': 'Salman', 'age': 'NA',
'crimes': 'lol', 'career_destroyed': 'uncountable'}
new_dict=> {'name': 'Salman', 'age': 'NA',
'crimes': 'lol', 'career_destroyed': 'uncountable'}
"""
But using these methods, have drawbacks.
d1.update(d2)
modifies d1 in-place i.e changes the content of d1 dictionary.
But the problem is, it is using a temporary variable internally to update d1 dict. This is very inefficient because consider a case when you want to merge two dictionaries containing thousands of key-value pair, It is a brutal waste of memory and CPU!
😛 Then why don't we use ** , because it is not readable and ugly!
Let's see how python3.9 solved the issue,
The merge operator , "|" will return a new dict
consisting of the left operand merged with the right operand, each of which must be a dict.
This operation has a fancy name : Dict Union
This operator performs an inplace merge similar to update method but it doesn't waste memory by using a temporary variable!
Fancy name : Augmented assignment
Prior to python3.9, you would do something like this,
# removing prefix or suffix prior to python 3.9
def remove_prefix(inp_str: str, pref: str) -> str:
"""
This function deletes "pref" from the
beginning if it is present otherwise
returns the original "inp_str"
"""
if inp_str.startswith(pref):
return inp_str[len(pref):]
return inp_str
def remove_suffix(inp_str: str, suff: str) -> str:
"""
This function deletes "suff" from the
end if it is present otherwise
returns the original "inp_str"
"""
if inp_str.endswith(suff):
return inp_str[:-len(suff)]
return inp_str
"""OUTPUT
>>> remove_suffix("Kathan", "an")
'Kath'
>>> remove_suffix("Kathan", "lol")
'Kathan'
>>> remove_prefix("Kathan","Ka")
'than'
>>> remove_prefix("Kathan","haha")
'Kathan'
"""
Isn't it too much, that we need to define functions for such a simple task? Ofcourse you can create a one-liner using lambda
but what if you want a cleaner solution?
To solve this issue, 2 new methods were added.
This method has implemented the prior discussed functionality of removing prefix for us so that we don't need to define our own function or lambda whatever.
This method has implemented the prior discussed functionality of removing suffix for us so that we don't need to define our own function.
Prior to python 3.9, this function only accepted two arguments and returned their gcd.
But quite often we need to find gcd of more than two numbers. GOOD NEWS! In python 3.9 you can add as many arguments you want into this function.
Prior to python 3.9 there was no specifc function to calculate l.c.m.
But if you ain't a math geek, you will be confused by the function 😓, So there was a need for l.c.m function!
That's it for this post :) Hope you enjoyed it. Check python 3.9 docs for lots of more fun stuff!
15