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
  • 2,794
  • 5
  • 24
  • 43

1 Answers1

10

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
  • 1,720
  • 19
  • 33
  • 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