[Tutor] Accessing List items

Matt Williams matthew.williams at cancer.org.uk
Mon Mar 21 12:57:21 CET 2005


Dear List,

Thanks for the help with the previous problem - now all solved!

I have a question about accessing lists. I'm trying to do something to
each item in a list, and then assign it back into the list, overwriting
the old value. However, the first loop here doesn't work, whereas the
second loop does - but is very ugly.

x = "200%  inv_nodes=0-2  node_caps=no"
y=x.split()
    
for i in y:
    i="z"
print y
    
for i in range(len(y)):
    y[i]="z"
    print y

Surely there must be a better way than this ?

Thanks,

Matt



More information about the Tutor mailing list