I'm very new to Python and are having problems with my code. I want to write a function that compares to lists and prints to the user, which elements exist in list1 but not in lis2.
For example the input can be:
list1=["john", "jim", "michael", "bob"]
list2=["james", "edward", "john", "jim"]
And then the output should be:
Names in list1, but not in list2: Michael, Bob
Names in list2, but not in list1: James, Edward
Thank you your help!
(EDIT: this is my code so far:
def compare_lists(list1, list2):
for name1 in list1:
if name1 not in list2:
print("Names in list1, but not in list2: ", name1)
for name2 in list2:
if name2 not in list1:
print("Names in list1, but not in list2: ", name2)
And my problem is that the output is printed twice:
Names in list1, but not in list2: Michael
Names in list1, but not in list2: Bob
Names in list2 but not in list1: James
Names in list2 but not in list1: Edward