Python Tricks for Data Science and Data Analysis — Part 6
This is the sixth part of my Python Tricks
A couple of weeks ago, I shared some Python and Pandas tricks to help Data Analysts and Data Scientists quickly learn new valuable concepts that they might not be aware of.
These concepts are also part of the collection of tricks I share daily on LinkedIn.
For today, we are making a specific focus on six Python best practices and modules that are unknown by most people.
1. Elegantly merge dictionaries in Python
If you are using Python 3.9 or higher, then maybe it is time to start using this technic when merging dictionaries.
For instance instead of writing for loops, you can elegantly merge them using simple union operators: |
Let’s see how it works considering the following three lists. The goal is to join them in order to have a unique list.
natural_wonders = {'Serengeti': 'Tanzania', 'Ngorongoro': 'Tanzania'}
iconic_landmarks = {'Pyramids': 'Egypt', 'Victoria Falls': 'Zambia/Zimbabwe'}
cultural_treasures = {'Lalibela': 'Ethiopia', 'Timbuktu': 'Mali'}
The merge approach before Python 3.9
would be to use a for loop as follows: