Every now and then we come across List and Dict operations in our code. Python gives great syntactical sugar to work with List and Dicts.
Whether it is initializing a new list or transforming an existing list, list comprehension is often very handy.
Consider the following code:
Now we want to capitalize the fruit names. Instead of writing multi liner for loops like this
We can achieve the same thing in a single liner with Python’s list comprehension:
This is awesome. The syntax to remember is:
output = [expression for item in collection] or output = [doSomething()…
ML Engineer | Full Stack Developer | Technology Enthusiast | Sports Lover