Multiple if-statements with overwriting lists I currently have the following piece of code in place that attempts to find matches within a list, based on a priority system:
possible_combinations = [&quot;ABC&quot;, &quot;AB&quot;, &quot;A&quot;, &quot;D&quot;]


if len(possible_combinations) &gt;= 1:
    chosen_combination = [comb for comb in possible_combinations if (&quot;A&quot; in comb) and (&quot;C&quot; in comb)]
    if (len(chosen_combination) == 0):
        chosen_combination = [comb for comb in possible_combinations if (&quot;A&quot; in comb)]
        if (len(chosen_combination) == 0):
            chosen_combination = [comb for comb in possible_combinations if (&quot;C&quot; in comb)]
            if (len(chosen_combination) == 0):
                raise ValueError(f&quot;No match found.&quot;)
                
print(chosen_combination)

['ABC']

Is there any way I could refactor this code to get rid of these if-statements, and thereby make my code look nicer?
You could do something like this:
possible_combinations = [&quot;ABC&quot;, &quot;AB&quot;, &quot;A&quot;, &quot;D&quot;]
combs = [(&quot;A&quot;,&quot;C&quot;),(&quot;A&quot;,),(&quot;C&quot;,)]

for comb in combs:
    chosen_comb = [x for x in possible_combinations if all([y in x for y in comb])]
    if len(chosen_comb) &gt; 0:
        break

print(chosen_comb)