Home Artificial Intelligence Complex List Comprehensions Can Be Readable!

Complex List Comprehensions Can Be Readable!

0
Complex List Comprehensions Can Be Readable!

PYTHON PROGRAMMING

Even quite complicated Python comprehensions could be more readable than the corresponding for loops.

Towards Data Science
Python comprehensions allow for powerful computations in loops — even nested ones. Photo by Önder Örtel on Unsplash

Python comprehensions — including list, dictionary and set comprehensions in addition to generator expressions — constitute a strong Python syntactic sugar. You may examine them in the next articles:

Python comprehensions have two great benefits when put next to the corresponding for loops: they’re faster and so they could be far more readable.

Note the phrase “could be far more readable.” Indeed, they aren’t all the time more readable. This begs the next query: When are they?

It will depend on you. It’s you — the developer — who makes a Python comprehension readable. Sloppy implementation can destroy a comprehension’s readability, though the identical could be said about for loops.

Comprehensions in Python were designed to be read in a really similar way that you simply read an English sentence: You may read a comprehension from left to right (or from top to bottom, if it takes several lines) identical to you read an English sentence from left to right.

Many say you shouldn’t use complex comprehensions because they’re difficult to read and comprehend. In this text, we’ll discuss this well-known principle — if not a myth. Unfortunately, many individuals strain this principle by excessively avoiding the usage of Python comprehensions in situations where they could possibly be used with success.

You may read a comprehension from left to right (or from top to bottom, if it takes several lines) identical to you read an…

LEAVE A REPLY

Please enter your comment!
Please enter your name here