-1

I have a list of string in python.

header_row = ['column 1', 'column 2', 'column 3']

I want to replace the empty space with underscore. The converted list will look like this;

converted_header_row = ['column_1', 'column_2', 'column_3']

The conversion can be done individually with the line below;

mystring.replace(" ", "_")

How do I iterate this replacement operation through all the strings in the list? I welcome any other method to do the conversion.

I am using python v3.6

user1315789
  • 3,069
  • 6
  • 18
  • 41

1 Answers1

2

You can do this in a list comprehension:

header_row = [mystring.replace(" ", "_") for mystring in header_row]
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80