Search code examples
pythonstringliststring-operations

Perform a string operation for every element in a Python list


I have a list of strings in Python - elements. I would like to edit each element in elements. See the code below (it doesn't work, but you'll get the idea):

for element in elements:
    element = "%" + element + "%"

Is there a way to do this?


Solution

  • elements = ['%{0}%'.format(element) for element in elements]