Hi
I have two lists list1 and list2 I want to extract all values that exist in list1 and don’t exist in list2.
I tried this code:
1 2 3 4 5 6 | list1 = ['lion', 'dog', 'cat', 'tiger', 'cat', 'cat', 'zebra'] list2 = ['monkey', 'tiger', 'cow', 'mouse'] my_list = [animal for animal in list1 if animal not in list2] print(my_list) |
and give me this result:
1 2 | ['lion', 'dog', 'cat', 'cat', 'cat', 'zebra'] |
How can i modify my code to have result without duplcate values like this:
1 2 | ['lion', 'dog', 'cat', 'zebra'] |
thanks
You can simly use a for loop with append to list.
kind regards
Hi,
to remove duplicates from the list you can add this line before printing the list:
best regards