How python uses for Loop to modify the value of an element in list

l = ["1", "2", "1", "1"]
for i in l:
    if i == "1":
        i = "3"
print(l)

Why is the result of l still ["1percent," 2percent, "1percent," 1"], and how can it be modified to ["3percent," 2percent, "3percent," 3"]?

Jun.28,2022

l = ['1percent,' 2percent, '1percent,' 1']
new_l = []
for i in l:

if i == '1':
    new_l.append(3)
else:
    new_l.append(i)

l = new_l
print (l)


-sharp 
for i in range(len(l)):
     if l[i] == '1':
          l[i] = '3'


-sharp 
for i, item in enumerate(l):
    if item == '1':
          l[i] = '3'

in addition, if you want to delete an element, it is recommended not to make changes directly on the original data. It is prudent to make a copy list.remove (value) to delete.


-sharp 
for idx, i in enumerate(l):
    if i == '1':
        l[idx] = '3'
-sharp 
l = ['3' if i=='1' else i for i in l]        
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1beda25-5322c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1beda25-5322c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?