Python-ing it better
Where I work; we are obsessed with python, we use it everyday and to us python is an art. Python does not force rules unto you. But there’s always a better way of doing it; to us, a pythonic way. This article will try to show off some cool tricks and good practices in python that you can use to level up in the art of python.
Using built in functions
Most of us start our coding career with C/C++. Which is great and all but it drills in a certain way of doing things that we carry with us throughout the year and that is “assign then modify”. Lets talk code
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> intVector;
for( int i=0; i < 5; i++) {
intVector.push_back (i);
}
return 0;
}
We declare an empty vector and then loop over it to populate. There’s nothing wrong with it and it works absolutely OKAY. Lets convert this to python
int_list: List[int] = []
for i in range(5):
int_list.append(i)
Nothing wrong here either. But

There’s gotta be a better, more pythonic way of doing this
int_list: List[int] = list(map(lambda i: i, range(5)))
Wow. The map function can call a given function for every element of a given iteratables. We can use this for loads of stuff like
dict_list: List[Dict[Literal["id", "session"],
Union[int, str]]] = [{"id": 1}, {"id": 2}]
mod_list = list(
map(lambda element: {**element, "session": True}, dict_list))
Cool right? Say we need to get all the elements with session=True. Let’s do it the boring way first
dict_list: List[Dict[Literal["id", "session"],
Union[int, str]]] = [{"id": 1}, {"id": 2, "session": True}, {"id": 3, "session": True}]
filtered_session: List[Dict[Literal["id", "session"],
Union[int, str]]] = []
for element in dict_list:
if element.get("session", False):
filtered_session.append(element)
Looks OKAY, gets the job done but ew, we need a pythonic way. Here’s one
dict_list: List[Dict[Literal["id", "session"],
Union[int, str]]] = [{"id": 1}, {"id": 2, "session": True}, {"id": 3, "session": True}]
filtered_list = list(
filter(lambda element: element.get("session", False), dict_list))
Wow.
What if you only need the first match? Don’t you worry, Don’t you worry child, python’s got a plan for you
filtered_first_match = next(
filter(lambda element: element.get("session", False), dict_list))
# Be careful of StopIteration Exception
Python has a plethora of built in functions that you can use to write clean self-explained code. Please use them and LEVEL UP.
Comprehending Comprehensions
Python is an art. There are a number of ways to do what you need to do. You’re the painter, python is the brush. Move it the way you want.
Comprehensions are one off lines in python that can do many things in a more clear, comprehensible, concise way.
Let’s get back to the first example from the previous section. How can we achieve the same thing with comprehension? Let’s talk code
int_list = [i for i in range(5)]
It can’t get any prettier than this ladies and gentlemen. This is the beauty of python. You can use list comprehensions with almost every data type. Need a dictionary? Well
dict_ints = {i: "wow" for i in range(5)}
Talk about beauty. And you can do this with generators as well
generatored = (i for i in ["a", "b"])
for gen in generatored:
print(gen)
And with sets too
set_ing = {i for i in range(4)}
Impressed? You should be.
Needle in the thread
Have you ever had to make a bunch of api calls and ended up doing it like this
import requests
list_of_urls = ["https://jsonplaceholder.typicode.com/todos/1",
"https://jsonplaceholder.typicode.com/todos/2",
"https://jsonplaceholder.typicode.com/todos/3"]
fetched_data = []
for url in list_of_urls:
fetched_data.append(requests.get(url).json())
OKAY. But unfortunately I don’t have a tree full of time to wait for these to come in one by one. So let’s clear this up
import requests
list_of_urls = ["https://jsonplaceholder.typicode.com/todos/1",
"https://jsonplaceholder.typicode.com/todos/2",
"https://jsonplaceholder.typicode.com/todos/3"]
fetched_data = list(map(lambda url: requests.get(url).json(), list_of_urls))
This is my reaction after seeing this

But I still don’t have time. Can you speed it up?
import requests
from concurrent.futures import ThreadPoolExecutor
list_of_urls = ["https://jsonplaceholder.typicode.com/todos/1",
"https://jsonplaceholder.typicode.com/todos/2",
"https://jsonplaceholder.typicode.com/todos/3"]
with ThreadPoolExecutor(max_workers=min(len(list_of_urls), 4)) as exe_cutiepie:
fetched_data = list(exe_cutiepie.map(
lambda url: requests.get(url).json(), list_of_urls))
I mean

I hope this was able to demonstrate the beauty of python and how you can LEVEL UP as a developer. There are some caveats and gotchas when it comes to shortening codes so remember to use them wisely.
No Comments
Sorry, the comment form is closed at this time.