You are using numbers
variable when creating a for
loop, but then you are comparing number
(no 's') variable.
Also, avoid using list
as a list name; give it some other name (e.g. a_list, or something descriptive).
Others have already provided you with good answers. Just in case you have a hard time understanding list comprehensions, this is what you tried to do:
old_list = [1, 5, 0, 0, 2]
new_list = []
for each_element in old_list:
if each_element == 0:
new_list.append(0)
else:
new_list.append(1) # what about negative numbers?
This is equivalent to:
new_list = [0 if each_element == 0 else 1 for each_element in a_list]
You can also "modify" (overwrite) existing list on the fly:
a_list = [0 if each_element == 0 else 1 for each_element in a_list]