nanounanue
nanounanue

Reputation: 8342

Remove all elements of a list that are substrings of other elements of the list in python

I have the following list:

people = ['John', 'Maurice Smith', 'Sebastian', 'Maurice', 'John Sebastian', 'George', 'George Washington']

As you can notice, John, Maurice, Sebastian and George are names or last names of the full names (Maurice Smith, Jogn Sebastian and George Washington).

I would like to get only the full names. Is this possible in python?

Upvotes: 0

Views: 60

Answers (2)

kindall
kindall

Reputation: 184181

# make set of first names from full names
firstnames = set(name.split[0] for name in people if " " in name)

# get names that aren't in the above set
people[:] = (name for name in people if name not in firstnames)

Upvotes: 0

David Robinson
David Robinson

Reputation: 78600

You can remove them with this list comprehension:

[p for p in people if not any(p in p2 for p2 in people if p != p2)]

This iterates over each person p, then checks the condition:

not any(p in p2 for p2 in people if p != p2)

This inner loop iterates over each person p2 (skipping the case where it is the same as p), and checks p in p2 (whether p is a substring).

Upvotes: 3

Related Questions