Search code examples
pythonpython-3.xregexlistlist-comprehension

Remove "A-Z." from every list entry if it's there in Python


Sorry this might be an easy question but struggling to find a best way. I have list like below, and am wanting to remove all parts that contain any letter and a . afterwards.

So I want to transform this.

list = ['A. Person', 'B. House', 'H. Normal', 'My House', 'Hello World']

And transform it in to this.

list = ['Person', 'House', 'Normal', 'My House', 'Hello World']

What would be the easiest way to do this? I initially thought about a startswith("") but couldn't think what to put in it seeing as all the beginning letters are different. Maybe regex is the best answer?

Many thanks in advance!


Solution

  • Assuming there is only every one period, you could split by ". " and take the last element.

    l = ['A. Person', 'B. House', 'H. Normal', 'My House', 'Hello World']
    l = [i.split(". ")[-1] for i in l]
    print(l)
    >>> ['Person', 'House', 'Normal', 'My House', 'Hello World']