filter()ing a dict
Sean Ross
sross at connectmail.carleton.ca
Thu Aug 7 10:45:07 EDT 2003
d = {"key 1": ["value 1", "value 2", "value 3", "value 4"],
"key 2": ["value 1", "value 2"],
"key 3": ["value2", "value 3", "value 4"],
"key 4": ["value 1", "value 2", "value 3", "value 4"]}
# using list comprehension
fd = dict([(k,v) for k,v in d.items() if len(v)==4])
print fd
# using filter
value = 1
fd = dict(filter(lambda item:len(item[value])==4, d.items()))
print fd
More information about the Python-list
mailing list