-3

I have this line of code, and I'm just wondering what it would be equivalent to as a for loop.

lst = [x for x in l if x !=0] + [x for x in l if x == 0]
Dan Mašek
  • 17,852
  • 6
  • 57
  • 85

1 Answers1

-1

addition of lists is concatenation so:

lst = []

for x in l:
    if x != 0:
        lst.append(x)
        
for x in l:
    if x == 0:
        lst.append(x)

more on that: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

Pascal K
  • 128
  • 1
  • 10