[Tutor] Re: looping through and comparing lists of dictionaries
pan@uchicago.edu
pan@uchicago.edu
Wed Apr 23 11:24:12 2003
> Message: 7
> From: "bkd" <bkd@graphnet.com>
> To: <tutor@python.org>
> Date: Wed, 23 Apr 2003 03:04:07 -0400
> Subject: [Tutor] looping through and comparing lists of dictionaries
[snip]
> What I have now is this:
>
> for girl in range(len(redheads)):
> if len(redheads)==0: break
> else:
> for line in range(len(olddata)):
> if len(redheads)==0: break
> elif olddata[line]['name']<>redheads[girl]: pass
> elif olddata[line]['hair']=='Redhead': del redheads[girl]
Try this:
olddata = [x for x in olddata if x['name'] not in redheads]
for i in olddata: print i
This gives you:
>>> {'hair': 'Brunette', 'role': 'Castaway', 'name': 'Maryann'}
{'hair': 'Blond', 'role': 'Genie', 'name': 'Jeanne'}
{'hair': 'Brunette', 'role': 'Genie', 'name': 'Serena'}
HTH
pan