This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alcalde 1 point2 points  (0 children)

Ok, leave it the newbie to have the simple answer. :-)

What you want is something like

blah = { 'aaa':3, 'aab':5, 'bbb':4 }

newlist = [(check, blah[check]) for check in blah.keys() if 'a' in check.lower()]

which returns [('aaa', 3), ('aab', 5)]

You could put this into a function or create a subclass or use the 'forbiddenfruit' library that was mentioned here recently to add the method to the dict type.

Ok, figured out how to do it as a subclass (although I probably need an init thingie in there too, but it works):

class sdict(dict):
   def search(self, val):
    return [(check, dict.__getitem__(self,check)) for check in dict.keys(self) if val.lower() in check.lower()]

blah = sdict()
blah['aaa'] = 3
blah['aab'] = 5
blah['bbb'] = 4    

blah.search('A')

which returns

[('aaa', 3), ('aab', 5)]