Modifying a list while iterating when programming with python [duplicate]

you can access and change the list value using index following way:

val= [1,2,3,4,5,6,50,110]
for i in xrange(len(val)):
    if val[i] >= 100:
       val[i] = "100 "                                                           
    elif val[i] >= 10 and val[i] < 100:
       val[i] = str(val[i])
    elif val[i] > 0 and  val[i] < 10:
       val[i] = " " + str(val[i])
    elif val[i] <= 0:
       val[i] = "    "    #insert 4 spaces
    else:
       val[i] = "    "    #insert 4 spaces if all else fails

print val

Leave a Comment