How to compare two lists and copy the differences into a third (python)?

Viewed 428

I'm a beginner in programming. I'm trying to compare two lists and save the differences in a third list.

How would you copy the incorrect values to a third list?

I'm sure there are more efficient ways of coding, but I need to work with what I have here (unless it can't be fixed).

mc_answers is [A, C, A, A, D] I'm trying to compare it to whatever the user input was, so if input_list was [A, A, A, A, A,], w_answers would be [C, D] (function user_input was assigned to input_list to make it easier to work with).

        def wrong_input(input_list, mc_answers):

        print()
        wrong_answers=[]

        for i in range (5):
            while input_list[i]!=mc_answers[i]:
                wrong_answers.append(input_list[i])
               
  

       return wrong_answers

And:

    def main():

        mc_answers=['A', 'C', 'A', 'A', 'D']
    
        display_test()


        input_list=user_input()
        print(wrong_input(input_list, mc_answers))


    def user_input():
        user_answers=[]
        print()
        for i in range (5):
            user_answers.append(input('\nAnswer'+str(i+1)+ ': '))
        
        return user_answers

    def wrong_input(input_list, mc_answers):

        print()
        w_answers=[]

        for i in range (5):
            while input_list[i]!=mc_answers[i]:
                w_answers.append(input_list[i])
                
  

       return w_answers

    main()
2 Answers

Do not try to come up with your own algorithm to determine the difference between two lists. You can determine the difference between two lists using a set. A set is a built in data type in Python which is called using either the set() function (similar to the list() function), or by using {}.

You can utilize the symmetric_difference method to determine the difference between both lists, or you can use the difference method to determine the difference in one list that isn't in another. Consider,

list1 = [3, 12, 7, 8]
list2 = [1, 5, 12, 3]
list3 = set(list1).symmetric_difference(list2)
print(list(list3))

This outputs:

[1, 5, 7, 8]

Which means that the numbers 1, 5, 7, and 8 are not common between both list1 and list2. Depending on what you desire, you could use the difference method:

list1 = [3, 12, 7, 8]
list2 = [1, 5, 12, 3]
list3 = set(list1).difference(list2)
print(list(list3))

This outputs:

[7, 8]

This means that 7 and 8 are in list1 but are not in list2. You need to keep in mind some consequences with sets, however. For instance, If you convert a list to a set, it will remove duplicates. See this:

list1 = [1, 1, 2, 3]
print(set(list1))

Which outputs:

{1, 2, 3}

For a better explanation of sets, see this.

Edit

If all you want is a function that accomplishes the example that you have given us, then sets are probably not the way to go because you are dealing with lists that have duplicates. You can figure out what goes into the range function using the len function.

def wrong_input(input_list, correct_answers):
    wrong_answers = []

    for i in range(len(correct_answers)):
        for j in range(len(input_list)):
            # Move to the next item in correct_answers if this is true:
            if input_list[j] == correct_answers[i]:
                break

            # If we hit this condition, then that means that we looped through all of input_list and there were no
            # items that were the equal an individual item in correct_answers
            if j == len(input_list) - 1:
                wrong_answers.append(correct_answers[i])

    return wrong_answers


mc_answers = ['A', 'C', 'A', 'A', 'D']
user_answers = ['A', 'A', 'A', 'A', 'A']

print(wrong_input(user_answers, mc_answers))

This outputs:

['C', 'D']

First of all, thanks to all those who helped. For anyone trying to find a simpler (though not efficient) way of doing it:

def wrong_input(input_list, mc_answers): # The two parameters are lists from main() and user_input()

    wrong_answers=[]
    for i in range(5):
        if input_list[i]!=mc_answers[i]:
            wrong_answers.append(input_list[i])

    return wrong_answers

I changed the while to an if statement after @Barmar pointed out that a while loop will never end in this case.

Unrelated points to keep in mind (in case anyone comes across them, as I have):

  • A greater range than the lists you're comparing (i.e., range(6) instead of range(5) in this code.) will result in the error list index out of range (Both lists in this code have a length of 5.)
  • The first version of my code resulted in the error function 'object' not subscriptable. @Barmar pointed out that I left out the parenthesis when referring to the function user_input input_list=user_input instead of input_list=user_input(), which caused this error.
Related