1

I'm trying to make a function that removes items from all_cards based on whats in the iss_answer array. My iss_answer array will change often in my program so I can't just type all_cards.remove('Professor Plum','Dinning Room','Rope').

iss_answer = ['Professor Plum','Dinning Room','Rope']

def computer_1(array):

    all_cards = ['professor plum','colonel mustard',
                 'mrs. white','mr. green','miss scarlet',
                 'mrs. peacock', 'revolver','wrench',
                 'candle stick','lead pipe','knife','rope',
                 'kitchen','hall','dinning room','lounge',
                 'study','billiard room',
                 'conservatory','library','ballroom']

    all_cards.remove(iss_answer) 
    print all_cards       


    return all_cards

computer_1(iss_answer)

2 Answers 2

4

Use Sets to reduce complexity

difference = list(set(all_cards) - set(iss_answer))
2

Why not fast list comprehension- Just change the word case to match both list if needed and strip leading and trailing spaces after all use list comprehension.

iss_answer = ['Professor Plum','Dinning Room','Rope']

def computer_1(array):

    all_cards = ['professor plum','colonel mustard',
                 'mrs. white','mr. green','miss scarlet',
                 'mrs. peacock', 'revolver','wrench',
                 'candle stick','lead pipe','knife','rope',
                 'kitchen','hall','dinning room','lounge',
                 'study','billiard room',
                 'conservatory','library','ballroom']

    s = [i for i in all_cards if i.strip().lower() not in map(str.strip,map(str.lower,iss_answer))]
    print s      


    return s

computer_1(iss_answer)

Output-

['colonel mustard', 'mrs. white', 'mr. green', 'miss scarlet', 'mrs. peacock', 'revolver', 'wrench', 'candle stick', 'lead pipe', 'knife', 'kitchen', 'hall', 'lounge', 'study', 'billiard room', 'conservatory', 'library', 'ballroom']

But if you do not need to maintain order, since a set is an unordered data structure, of the element in output list you can use answers as @bakkal and @Saif Asif mentioned. More at here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.