 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to search Python dictionary for matching key?
If you have the exact key you want to find, then you can simply use the [] operator or get the function to get the value associated with this key. For example,
Example
a = {    'foo': 45,    'bar': 22 } print(a['foo']) print(a.get('foo'))  Output
This will give the output:
45 45
Example
If you have a substring that you want to search in the dict, you can use substring search on the keys list and if you find it, use the value. For example,
a = {    'foo': 45,    'bar': 22 } for key in a.keys():    if key.find('oo') > -1:       print(a[key])  Output
This will give the output
45
Advertisements
 