maksymov
maksymov

Reputation: 493

Remove elements from list

I have a variable:

x = 4

And I have a list:

list = [{'name': u'A', 'value': '1'}, {'name': u'B', 'value': '4'}, {'name': u'C', 'value': '2'}]

How can I exclude/remove the element in list where value=x?

Upvotes: 0

Views: 168

Answers (1)

Donald Miner
Donald Miner

Reputation: 39893

A list comprehension is perfect for this.

[ k for k in list if int(k['value']) != x ]

You can also use filter, but I believe list comprehensions are preferred in terms of style:

filter(lambda p: int(p['value']) != x, list)

edit: noticed your values are strings, so I added an int conversion.

Upvotes: 8

Related Questions