if string1 or string1 in string3, replace for string4


I need to check a name for the occurrence of either one of two different strings. If either one of the 2 strings is found in the name, I need to replace the string with a 4th string.


I could do the following,

if 'Brązowy' in product_name:
    product_name = product_name.replace('Brązowy','Bruin') 
elif 'Brązowa' in product_name:
    product_name = product_name.replace('Brązowa','Bruin') 

which works fine.

Or I could do it a bit shorter,

if any(x in product_name for x in ['Brązowy','Brązowa']):        
    product_name = product_name.replace('Brązowy','Bruin').replace('Brązowa','Bruin')

which also works fine.


But I was wondering if I could do something like this,

if any(x in product_name for x in ['Brązowy','Brązowa']):        
    product_name = product_name.replace(('Brązowy','Brązowa'),'Bruin')

Anybody a solution?


I would make a function instead.

def fix_pname(pname, search, replace):
    for item in search:
        if item in pname:
            return pname.replace(item, replace)
    return pname

product_name = fix_pname(product_name, ['Brązowy','Brązowa'], 'Bruin')