[Python] convenient grammar — Comprehension

Seong-Am Kim
2 min readMay 6, 2020

I’m doing Python for my company’s business these days, and I’d like to introduce it because it has a convenient function that distinguishes me from other languages.

Python calls it ‘Comprehension’, which can be useful in dealing with certain behaviors in repetitive sentences.

Photo by Sneaky Elbow on Unsplash

In the process of crawling, I needed to process the data.

Crawling patterns have always been the act of putting unprocessed data into a specific variable.

For example, refer to the code below.

When I want to use specific data in an array, I usually write the code as above.

However, this method creates a hassle to write code of the same pattern several times as the number increases.

If you use ‘list comprehension’ to create a function with the same function as follows.

This is a completely identical implementation.

The entire iteration is wrapped in an array and the most forward variable becomes the element to enter the array. (product variable is applicable here.)

And you can write down the conditional statement at the end and it is okay to omit

It is also possible to add values to the dictionary in the same way.

I think it can be useful for simply putting a value in an array.

However,I don’t think this method is highly legible at all.

when the conditional statement of the variable to be contained is longer or other complicated processing is required, this method drops the reading, so it is not recommended.

--

--