sidney
sidney

Reputation: 2714

Delete element from list where sub-element matches string

I have this list:

persons_list = [tom, alice]

Where tom and alice are two instances of Person. When I use tom.sexe, I get the string "Male" and when I use alice.sexe, I get her sexe, thus "Female". I may have others persons instances in persons_list.

Now, I would like to delete an element from the persons_list where sexe = "Male" in a one line shot, but I didn't succeed.

So far, I only manage to create a list containing only the age of the persons_list: list(map(lambda x: x.age, persons_list)) , but it is different from what I'd like to do.

Upvotes: 0

Views: 58

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124768

Use a list comprehension with a filter to rebuild the list:

persons_list = [person for person in persons_list if person.sexe != 'Male']

This picks out all objects that are not male.

Upvotes: 1

Related Questions