Search code examples
pythonlistloopscapitalize

Can Python remove elements from a list that have any uncapitalized word?


thanks for looking. I'm still working on my named entity recognition project, and I'm almost done. My project was to extract all the names of people from a long string, and I've gotten to the point where I have a list of names, which I have named ent3.

This list has some artifacts from previous processing that are incorrect. Specifically, I have elements in the list like 'Josie husband' or 'Laura fingernail'. I want to eliminate those elements completely.

Is there a way to make Python iterate over the list and remove any elements that contain an UNcapitalized word?


Solution

  • Here we are

    names = [
        'Jose Maria',
        'Alex Qqq',
        'Alex daddy',
        'Maria Fernandez',
        'Joe Dohn',
        'Dani mother'
    ]
    
    clean = [x for x in names if not any((word.lower() == word for word in x.split()))]
    
    print(clean)
    

    Outputs

    ['Jose Maria', 'Alex Qqq', 'Maria Fernandez', 'Joe Dohn']