Python dictionary search values for keys using regular expression

If you only need to check keys that are starting with "seller_account", you don’t need regex, just use startswith()

my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}

for key, value in my_dict.iteritems():   # iter on both keys and values
        if key.startswith('seller_account'):
                print key, value

or in a one_liner way :

result = [(key, value) for key, value in my_dict.iteritems() if key.startswith("seller_account")]

NB: for a python 3.X use, replace iteritems() by items() and don’t forget to add () for print.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)