Stack Exchange
Methods to shorten ifs in python using OOP or other patterns
I asked several people how I could shorten the ifs in python, but I did not find a clear answer
For example I have the following code below
if 'attributes' not in data:
    data['attributes']: list = []

    # filter columns with attributes
    attributes: group.filter = group.filter(like='attribute')

    # we take a maximum of 2 of the most appeared attributes
    for index, value in attributes.count().nlargest(self.nlargest_without_actions).items():
        data['attributes'].append(index.split('attribute-')[1])

if 'categories' not in data:
    data['categories']: list = []

    # filter columns with categories
    categories: group.filter = group.filter(like='category')

    # we take a maximum of 2 of the most appeared categories
    for index, value in categories.count().nlargest(self.nlargest_without_actions).items():
        data['categories'].append(index.split('category-')[1])

if 'filters' not in data:
    data['filters']: list = []

    # filter columns with filters
    filters: group.filter = group.filter(like='filters')

    # we take a minimum of 2 of the most appeared filters
    for index, value in filters.count().nlargest(self.nlargest_without_actions).items():
        data['filters'].append(index.split('filters-')[1])

if 'disliked_products' not in data:
    data['disliked_products']: list = []

    dislike_products: group.filter = group.filter(like='dislike-product')
    # we take a minimum of 4 of the most disliked products
    for index, value in dislike_products.count().nlargest(self.nlargest_with_actions).items():
        data['disliked_products'].append(index.split('dislike-product-')[1])

And the code still continues with 3 ifs
What method would you use to shorten those ifs?
I use them because I won't always have those values
How would you proceed more dynamically?
I tried to use a match, but it's not really a good solution
Another solution would be to group as many functions as in oop with get and the name of the attribute
for example
get_attributes,
get_filters..


Pay attention to the code in all your if blocks. Notice what remains the same. Notice what changes. The thing(s) that change can be hard coded in a list (of strings, or of tuples containing multiple strings if multiple things change), and the common code can be plopped into a loop that iterates over this list. You don't need OOP here. Do you know about loops? If not, you might find it helpful to go through a tutorial instead of asking Stack Overflow to do this one problem for you

words = {'attributes': 'attribute', 
         'categories': 'category', 
         'filters': 'filters', 
         'disliked_products': 'dislike product'}

for word, singular_word in words.items():
    if word in data:
        continue

    data[word]: list = []

    # filter columns with categories
    groups: group.filter = group.filter(like=singular_word)

    # we take a maximum of 2 of the most appeared categories
    for index, value in groups.count().nlargest(self.nlargest_without_actions).items():
        data[word].append(index.split(f'{singular_word}-')[1])


I thought about that too, but I won't have those words in the data.. for example attributes, categories. but I think you enlightened me a little towards another solution :)
