samedi 10 septembre 2016

condensing multiple if statements in python

I'm trying to write a function to check if an object is found in multiple lists and to remove the object from any list its found in. I want to know if there is a way to make it cleaner or smarter using some form of generic variable where you predefine the format or something along those lines. my code in its ugly form:

def create_newlist(choice):

if choice in list_a:
    list_a.remove(choice)
if choice in list_b:
    list_b.remove(choice)
if choice in list_c:
    list_c.remove(choice)
if choice in list_d:
    list_d.remove(choice)
if choice in list_e:
    list_e.remove(choice)

What I'm hoping for is something like:

if choice in list_x:
   list_x.remove(choice)

I would like it to work for each list, would I need to loop through? any suggestions would be great! I have the workaround but I would love to learn a more elegant way of coding this!

Aucun commentaire:

Enregistrer un commentaire