1

My question is not related to a problem, as such, but I looked at several python scripts, like the one below:

''.join([i if ord(i) < 128 else '' for i in text])

The list is built on a loop and contains an IF statement. I tried to find in the documentation the structure of such formula (e.g why put the IF in front and the FOR at the end). I am trying to understand the logic behind, in order to be able to build and develop my own formula. Unfortunately, despite all the documentation I looked on the net and books I bought, the information was pretty basic (usually they use enumerated lists and that's it). Could any of you give me a link to a doc that would be a bit more explicit on this topic ?

I recently discovered the dict(zip(a,b))-way to build dictionary, but the lack of understanding of this topic keeps me behind...

Best Regards,

hiro protagonist
  • 36,147
  • 12
  • 68
  • 86
JCF
  • 143
  • 10

1 Answers1

1

Those are List Comprehensions and are pretty much a condensed for loop to cover common loop patterns with less code. ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions )

  • If anyone else got the same question, now that I have the right keyword and starting point from C.Bluoss (principle is clear now), I will keep going with the 2 following links that go a bit beyond in terms of complexity: http://python-3-patterns-idioms-test.readthedocs.io/en/latest/Comprehensions.html and http://www.python-course.eu/python3_list_comprehension.php – JCF Jan 25 '17 at 16:34