Rilcon42 Rilcon42 - 4 months ago 111
Python Question

Iterating over dictionary with wildcard key matching

I am trying to iterate over a dictionary in order to retrieve all values that match the pattern

npi[0-9]+
for the key. Is this possible in Python?

for data in input_file:
print(data['npi0'])
print(data['npi1'])
....

Answer

You just iterate over your dictionary, and do a pattern match on the key.

for key in dict
    if re.match(r'npi[0-9]+', key):
        print(dict[key])
Comments