How to add characters to a specific element in a list without overwriting the entire list?

Okay, so my original problem was adding characters to a specific element in a list, but then I encountered the problem of my fix overwriting my entire list, leaving me with the edited element.

My code

listy = ['item_1','item_2','item_3']

listy = ['('+listy[1]+')']


I understand why this is happening, I just don’t know how to also add back the rest of the list (non-manually)

I tried doing:

listy = [listy[0:1] + '('+listy[1]+')' + listy[2:]]

And got back a TypeError, which I expected, but wanted to make sure it didn’t work first.

This was all I tried.

>Solution :

Assign to the list index, not the variable holding the entire list.

listy[1] = '('+listy[1]+')'

Leave a Reply