3

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
Madman12
  • 37
  • 4
  • Hello, welcome to stack overflow. We will be glad to answer your question, but we would first like to show us what you have tried and why it hasn't worked. Otherwise, we won't be helping you with your problem; we will just be doing your work for you. Just as a minor spoiler, what you need can be done in a single line using some pyhton features. – kyriakosSt Oct 28 '18 at 12:53
  • could you please edit your question to include your code formatted there? – kyriakosSt Oct 28 '18 at 12:55
  • Nice, you can in fact find the answer you are looking for in this post: https://stackoverflow.com/questions/6486450/python-compute-list-difference – kyriakosSt Oct 28 '18 at 13:00
  • FYI, your code is actually working. Your second print statement has the same text with the first one, maybe that's why you got confused. All you had to do print the names in the same row was to append them to two lists instead of printing them – kyriakosSt Oct 28 '18 at 13:08
  • 2
    Possible duplicate of [Python, compute list difference](https://stackoverflow.com/questions/6486450/python-compute-list-difference) – kyriakosSt Oct 28 '18 at 13:08
  • Thank you! And whoops.. typo.. But I would like that the output is strings: "michael", "bob" and not strings in list ["michael", "bob"]. – Madman12 Oct 28 '18 at 13:12
  • If you have your list of strings, you can then go through it to print them one by one – kyriakosSt Oct 28 '18 at 13:20

2 Answers2

2

Try this:

list1=["john", "jim", "michael", "bob"]
list2=["james", "edward", "john", "jim"]

names1 = [name1 for name1 in list1 if name1 not in list2]
names2 = [name2 for name2 in list2 if name2 not in list1] 
print(names1)
print(names2)
0

You could store the result in a temp string, then print them out.

def compare_lists(list1, list2):

    str1 = ''
    for name1 in list1:
        if name1 not in list2:
            str1 += name1 + ' '
    print("Names in list1, but not in list2: ", str1)

    str2 = ''
    for name2 in list2:
        if name2 not in list1:
            str2 += name2 + ' '
    print("Names in list1, but not in list2: ", str2)

list1=["john", "jim", "michael", "bob"]
list2=["james", "edward", "john", "jim"]

compare_lists(list1, list2)
djy
  • 737
  • 6
  • 14