9

I have a list composed with my defined items, each of which has a attribute .name

t = [item1, item2]

I want to remove item from the t list according to their attribute .name, like remove() or pop() methods. Maybe I can do something like:

t.remove(item.name=="Removed me")

Maybe I don't need to go through the whole list to filter out the item needed to be removed.

Liao Zhuodi
  • 3,144
  • 5
  • 26
  • 46

1 Answers1

11

List comprehension works well for this kind of stuff

t = [i for i in t if i.name!="Remove me"]

Indeed, as commented, it creates a new list

yota
  • 2,020
  • 22
  • 37
  • 3
    Note that it replaces the list object, which may or may not be acceptable in any given usecase. –  Nov 30 '15 at 10:13