python integer value replacement in a list not working properly

Question:

I am trying to replace a few integers from a list with a specific value. However, the output which I am getting is not desirable. Is the for loop not working properly or is there some other problem?

y1= [1,2,3,-1,-2,-3,-4,0,0]

for i in range(len(y1)):
    if y1[i]<0:
        y1[i]==0
        
print(y1)

output:

[1, 2, 3, -1, -2, -3, -4, 0, 0]

Why is not the output the following:

[1, 2, 3, 0, 0, 0, 0, 0, 0]
Asked By: MRR

||

Answers:

Instead of using double "==" you should use "=".

y1= [1,2,3,-1,-2,-3,-4,0,0]

for i in range(len(y1)):
    if y1[i]<0:
        y1[i]=0
        
print(y1)
Answered By: RafaƂ
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.