One of the problems in my curriculum states to create a function that returns a copy of a list with the non-true elements removed. After reviewing the solution, I didn't understand the meaning of this line of code in the solution:
[val for val in lst if val]
I understand loops and if statements, however this does not make sense to me. I have not been able to find anything within my curriculum about this or the internet. If anyone understands and can explain, I would appreciate it. The entire function solution is below
def compact(lst):
"""Return a copy of lst with non-true elements removed.
>>> compact([0, 1, 2, '', [], False, (), None, 'All done'])
[1, 2, 'All done']
"""
return [val for val in lst if val]