How can I print a variable stored in a dictionary using a for-loop?


I have two dictionaries as follows:

superhero_dict  = {
    u'phone_number_4': u'07400000000',
    u'phone_number_3': u'02000000000',
    u'phone_number_2': u'02010000000',
    u'phone_number_1': u'07500000000',
    u'full_name': u'Bruce Wayne'

superhero_dict =    {
    u'phone_number_3': u'02000000001',
    u'phone_number_2': u'02010000001',
    u'phone_number_1': u'07500000001',
    u'full_name': u'Peter Parker',
    u'secret_name': u'Spiderman'

I would like to have a for loop that prints the phone numbers of each superhero to screen. The complication is that each dictionary has a different number of (a) keys and (b) phone numbers. The format of the phone number key is consistent i.e. "phone_number_" + integer as shown in the examples.

The best I could come up with was

for secret_number in range(1,10):
        print superhero_dict["phone_number_"+str(secret_number)]

There are two issues with this approach:

  1. I don't want to use some arbitrary upper limit (10 in the above example)

  2. It doesn't seem very elegant/pythonic

You can tweak it so there's no limit:

import itertools

for num in itertools.count(1): # count up from 1 infinitely
    phone = superhero_dict.get('phone_number_' + str(num))
    if phone is None:
    print phone