Reputation: 1
I came across the below line in a piece of code written in python. Couldn't understand it. Please elaborate.
pancake_row = [p == '+' for p in line.split()[0]]
Upvotes: 0
Views: 33
Reputation: 1299
Well, it's a list comprehension which is a bit like a condensed for loop which only ever returns a list.
line
is a str (but we only know this from the code because 'split' is a str method)
line.split()
generates a list from the str (splitting at whitespace)
line.split()[0]
is the first element of that list;
p == '+'
returns a boolean, True or False, and will only ever run once;
So the only possible output is [True]
or [False]
Upvotes: 1