dimanche 17 mai 2020

Iterating through two lists with 'if... or' statement

I have 3 lists.

values_to_add = []
values_to_remove = []
values_to_keep = []

Only one of the lists is populated at a time. If one of two of the lists is populated, I would like to perform this action:

if values_to_remove or values_to_keep:
    if values_to_remove:
        for value in values_to_remove:
            try:
                value_object.related_value.get(value=value)
            except exceptions.ObjectDoesNotExist:
                raise UnableToUpdate("These values are not related!")
    elif values_to_keep:
        for value in values_to_keep:
            try:
                value_object.related_value.get(value=value)
            except exceptions.ObjectDoesNotExist:
                raise UnableToUpdate("These values are not related!")        

However this code seems clunky and I am sure there is a more concise way to write this as it is literally repeating code. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire