Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python integer value replacement in a list not working properly

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[1, 2, 3, 0, 0, 0, 0, 0, 0]

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading