Say I have an array:
array=[1,0,2,3,4,0,0,5,6,0]
I want a list that returns just the numbers and not zeros. So I did this and it works:
print(list(y for y in array if y!=0)
I tried another way without list comprehension and it won't work, can someone explain why?
for y in array:
if y!=0:
print(list(y))
What would be another way to print a list of just the numbers without the 0's?
Edit: I tried to solve this problem using a for loop, and it works if I make an empty list on top. It works, but I don't understand why! But why does this work and other one doesn't? What's more efficient, this or the list comprehension in terms of speed/memory?
array=[1,0,2,3,4,0,0,5,6,0]
list=[]
for y in array:
if y!=0:
list.append(y)
print(list)
Lets take a good look at what you thought was right:
for y in array:
if y!=0:
print(list(y))
So we go through every value in the array. If the value isn't zero, we print list(y). The problem starts here. Because y is a integer, list(y) returns an error, because you can't convert an integer into a list. It would work if you did print(y).
But then comes another problem. If we print every element of the list which isn't a zero, we get something like this, because that code would only print in order:
1
2
3
4
5
6
You state in your question that you wanted a list. So this code wouldn't work either, because there would be no stored list. So we finally arrive to the right answer:
array=[1,0,2,3,4,0,0,5,6,0]
list=[]
for y in array:
if y!=0:
list.append(y)
print(list)
This answer stores each y value which isn't zero in a list, then finally prints out the list.
EDIT:
This is how the list comprehension works:
First of all, I can't help but observe that you have made a syntax error. :P You forgot an ending parantheses! Here's the correct code:
print(list(y for y in array if y != 0)).
Second of all, I need to state that that is NOT A LIST COMPREHENSION. That is a generator. There is a tiny difference.
A list comprehension generates the list on the spot. A list comprehension looks like this: [y for y in array if y != 0]
A generator, which is what you used above, is an expression is stored. It looks like this: y for y in array if y != 0.
So instead of using list(y for y in array if y != 0, you can directly go to [y for y in array if y != 0].
So now I will explain how the "list comprehension" (generator expression) actually works. It starts out by looping through each value of array. It checks if the value is not a zero. If it isn't, it adds y to the output list. So basically, the generator expression is the same as the second working code with the for loop, except python creates the output list for you, adding some convenience.
If you want to do it with a for loop you need to do something like this:
nonzero = []
for y in array:
if y != 0:
nonzero.append(y)
print(nonzero)
but a list comprehension or a filter will be better, both in terms of readability and in terms of speed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With