Skip to content
Advertisement

How do I remove a part of a string?

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.

Advertisement

Answer

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.

Advertisement