DEV Community

Mauricio Panelo
Mauricio Panelo

Posted on

Why You Should Start Using Listcomps in Python (Say no to map/filter)

Listcomps versus map/filter

Yesterday, I shared a quick Python tip about for-loops versus list comprehensions.

Today, I created another infographic about list comprehensions (listcomps). Listcomps provide a readable alternative to map/filter comprehensions. As you can see from the infographic, the listcomp syntax is shorter and easier to understand.

This syntax isn't limited to lists either.

You can do dict and set comprehensions too

Here's a short example of a dict comprehension:

country_codes = [
    ("United Kingdom", 44),
    ("United States", 1),
    ("Mexico", 52),
    ("Australia", 61),

code_to_country = {code: country for country, code in country_codes}
Enter fullscreen mode Exit fullscreen mode

The tools I use to create the infographic

And that's it! I encourage you to start creating your own infographic!

Thanks for reading! Let me know if you have any feedback.

Top comments (0)