I want to remove values from a dictionary if they contain a particular string and consequentially remove any keys that have an empty list as value.
mydict = {
'Getting links from: https://www.foo.com/':
[
'+---OK--- http://www.this.com/',
'+---OK--- http://www.is.com/',
'+-BROKEN- http://www.broken.com/',
'+---OK--- http://www.set.com/',
'+---OK--- http://www.one.com/'
],
'Getting links from: https://www.bar.com/':
[
'+---OK--- http://www.this.com/',
'+---OK--- http://www.is.com/',
'+-BROKEN- http://www.broken.com/'
],
'Getting links from: https://www.boo.com/':
[
'+---OK--- http://www.this.com/',
'+---OK--- http://www.is.com/'
]
}
val = "is"
for k, v in mydict.iteritems():
if v.contains(val):
del mydict[v]
The result I want is:
{
'Getting links from: https://www.foo.com/':
[
'+-BROKEN- http://www.broken.com/',
'+---OK--- http://www.set.com/',
'+---OK--- http://www.one.com/'
],
'Getting links from: https://www.bar.com/':
[
'+-BROKEN- http://www.broken.com/'
]
}
How can I remove all dictionary values that contain a string, and then any keys that have no values as a result?