Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Remove dictionary from list if key is equal to value [duplicate]

How do I remove from list a all dictionaries which link value is included in list b?

a = [{'link':'http://example.com/1/', 'id': 1}, {'link':'http://example.com/2/', 'id': 2}]
b = ['http://example.com/2/', 'http://example.com/3/']

a should be:

a = [{'link':'http://example.com/1/', 'id': 1}]
like image 208
Hyperion Avatar asked Feb 15 '17 21:02

Hyperion


Video Answer


1 Answers

a = [x for x in a if x['link'] not in b]

Demo:

>>> a = [{'link':'http://example.com/1/', 'id': 1}, {'link':'http://example.com/2/', 'id': 2}]
>>> b = ['http://example.com/2/', 'http://example.com/3/']
>>> a = [x for x in a if x['link'] not in b]
>>> a
[{'link': 'http://example.com/1/', 'id': 1}]
like image 121
Uriel Avatar answered Oct 11 '22 09:10

Uriel