0

I'm a python newbie. I come from a C/C++ background and it's really difficult for me to get my head around some of python's concepts. I have stumbled upon this block of code which just plain confuses me:

file_names = [os.path.join(label_directory, f)
                   for f in os.listdir(label_directory) 
                   if f.endswith(".ppm")]

So, it's an array that joins label_directory with a variable f (both strings), which is initially uninitialized. The for loop then populates the variable f if the condition f.endswith(".ppm") is true.

Now, from my C/C++ perspective I see this:

A for loop that has an if statement that returns True or False. Where is the logic that excludes all the files that don't end with ".ppm" extension?

Sheldore
  • 37,862
  • 7
  • 57
  • 71
Shibalicious
  • 288
  • 2
  • 4
  • 14
  • 5
    In Python what you are seeing is called a "list comprehension". That is a broad subject, so I recommend doing an internet search for "Python list comprehension" and you will find numerous explanations. – Matt Runion Sep 21 '18 at 13:21
  • 2
    The `join` is only executed for the items that pass the condition. In `c` terms, think about the `join` as if it was placed at the end of the statement instead of the start. – pault Sep 21 '18 at 13:23
  • @mrunion Thank you guys, doing research as of now! – Shibalicious Sep 21 '18 at 13:24
  • 1
    This is a list comprehension. The first set of code on the right hand side is the code you put inside the if statement that you want to execute if the condition is True. The next set of code is the for loop over all the directories and then you check using if statement for your condition. Please note that if you would have had both if and else in this code, then the if and else will be placed before the for loop. If you expand the code above in different lines, you would first write for loop, then the if statement and then the `os.path.join(label_directory, f)` – Sheldore Sep 21 '18 at 13:24
  • 1
    For starters: http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ . – 9769953 Sep 21 '18 at 13:25
  • Thank you all a lot for your comments, I will do my research! – Shibalicious Sep 21 '18 at 13:26

2 Answers2

1

This syntax is called list comprehension. It constructs a list by evaluating expression after the opening square bracket for each element of the embedded for loop that meets criteria of the if.

piokuc
  • 25,594
  • 11
  • 72
  • 102
1

This is called a list comprehension. Python defines list comprehensions as

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

Syntactically, the code you gave is the same as

file_names = []
for f in os.listdir(label_directory):
    if f.endswith(".ppm"):
        file_names.append(os.path.join(label_directory, f))

If you want to learn more about list comprehensions, you can find out more here: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

dangee1705
  • 3,445
  • 1
  • 21
  • 40