I have a list and I would like to make a new list where newlist[i] = 'literal' + oldlist[i].
I have tried code like:
i = 1
for elem in oldlist:
newlist[i] = 'literal' + oldlist[i]
i += 1
i = 1
for elem in newlist:
elem = 'literal' + oldlist[i]
i += 1
and many similar attempts yet keep getting errors such as “can’t assign to function call” and others. How should my code be?
Lists are zero-based meaning the first item in the list is at index
0.e.g.,
You are starting your indexing at
1hence your first item never gets changed.Though your code doesn’t fit the canonical “indexed-loop” format. It should look more like this:
But an even better way to do this is to use a list comprehension: