I have a list of cities in the United States (some elements: 'Midlothian, VA', 'Ridley Park, PA', 'Johannesberg, CA', 'Sylva, NC', 'Kewwannee, IL', 'Superior, WI', 'Brewster, OH', 'Stillwater, MN'
). I've been trying to remove anything after the comma (since I only want the name, not the state). I tried many things, but I can't get it to work.
For example:
for i in US_cities_list:
i = i.split(",")
if len(i) == 2:
x = i.pop(1)
i = x
This still gives me the same output ('Midlothian, VA', 'Ridley Park, PA', 'Johannesberg, CA', 'Sylva, NC', 'Kewwannee, IL', 'Superior, WI', 'Brewster, OH', 'Stillwater, MN'
). Any help would be appreciated, thanks.
CodePudding user response:
The problem with your solution is that assigning i = x
doesn't modify
the original list. You can see this by adding print(US_citites_list)
inside your loop and observe that the list never changes.
One solution is to build a new list with the modified strings:
cities_only = [i.split(",")[0] for i in US_cities_list]
Note that I take advantage of the fact that i.split()
will always return a list of at least one element. If there is no comma in the string, there will only be one element. If there is a comma, then there will be two elements. This means there is no need to check the length of i
.
CodePudding user response:
A regex replacement works nicely here:
inp = ['Midlothian, VA', 'Ridley Park, PA', 'Johannesberg, CA', 'Sylva, NC', 'Kewwannee, IL', 'Superior, WI', 'Brewster, OH', 'Stillwater, MN']
output = [re.sub(r',.*', '', x) for x in inp]
print(output)
This prints:
['Midlothian', 'Ridley Park', 'Johannesberg', 'Sylva', 'Kewwannee', 'Superior',
'Brewster', 'Stillwater']
CodePudding user response:
As Code-Apprentice pointed out, assigning i = x
doesn't modify the original list.
One method of actually modifying the original list while keeping as much of your original code as possible would be:
for i, item in enumerate(US_cities_list):
item = item.split(",")
US_cities_list[i] = item[0]