Find dictionary items whose key matches a substring

Abid A picture Abid A · May 7, 2012 · Viewed 88.4k times · Source

I have a large dictionary constructed like so:

programs['New York'] = 'some values...' 
programs['Port Authority of New York'] = 'some values...' 
programs['New York City'] = 'some values...'
...

How can I return all elements of programs whose key mentions "new york" (case insensitive)? In the example above, I would want to get all the three items.

EDIT: The dictionary is quite large and expected to get larger over time.

Answer

mensi picture mensi · May 7, 2012
[value for key, value in programs.items() if 'new york' in key.lower()]