Search in list of dictionaries

Alex Gusarov amgusarov at gmail.com
Thu Feb 19 12:17:56 EST 2009


 Hello everybody!

I've a list of dictionaries with 'shorcut' and 'command' keys. When user
types a word program must search this list for a typed shortcut and then run
linked command. What I've wrote:

        for cmd in self.commands:
            if cmd['shortcut'] == input:
                os.popen(cmd['command'])
                break
        else:
            os.popen(input)

But it's a brute-force method and I think there is another way in searching
items through a list by dictionary key. Please give me advice how can I
implement fast search in list of dictionaries by some dictionary key. In my
mind language:

list.get({'shortcut' == input})

Thanks a lot, Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090219/f76b022f/attachment.html>


More information about the Python-list mailing list