I've never used List Comprehensions but have spent the last few hours trying to apply what I've read to a method I'm working on. It's difficult for me to apply the examples I've seen to my case.
Given a list, create a new list of equal size. In that new list, shift all the numbers to the left with zeroes to the right. So for example, [0, 3, 0, 4] will return [3, 4, 0, 0]. I have a working method that does this:
def merge(self, line):
results = [0] * len(line)
count = 0
for number in line:
if number != 0:
results[count] = number
count += 1
return results
Each time I try my hand at compressing that, I get stumped by how to accomplish results[count] = number
without an index.