I've been doing a school project to automate product category labelling. I am using python and need a simplify code using loops and dictionary, but I am new to Python and my code looks very long and inefficient. So basically I have a product names, and I want to automate the product category labelling. I tried to detect the words in the product names using regex. For example, product named "Daylight Facial Cleanser" which contains "cleanser" will be labelled "Face Wash", and then "Dry Skin Moisturizer Gel" which contains "moisturizer" will be labelled "Moisturizer", etc. Here's my function : def labelling(text): cleanser = re.findall(re.compile(r'^(?=.*cleanser)+', flags=re.I | re.X), text) face_wash = re.findall(re.compile(r'^(?=.*face)(?=.*wash).+', flags=re.I | re.X), text) mask = re.findall(re.compile(r'^(?=.*mask)+', flags=re.I | re.X), text) moisturizer = re.findall(re.compile(r'^(?=.*moisturizer)+', flags=re.I | re.X), text) if cleanser: return "Face Wash" elif face_wash: return "Face Wash" elif mask: return "Mask" elif moisturizer: return "Moisturizer" else: return " " df['category']=df['product_name'].apply(lambda x : labelling(x)) can someone explain a simpler function using loops and dictionary so we only input the words and the label in the dictionary, and then call it inside the loops? Continue reading...