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)
Zach Stow
  • 157
  • 1
  • 11

2 Answers2

4

Use Sets to reduce complexity

difference = list(set(all_cards) - set(iss_answer))
Saif Asif
  • 5,516
  • 3
  • 31
  • 48
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.

Community
  • 1
  • 1
Learner
  • 5,192
  • 1
  • 24
  • 36