List Difference
Find elements that are in the first list but not in the second.
BeginnerTopic: List Programs
Python List Difference Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to find list difference (A - B)
list1 = input("Enter first list elements: ").split()
list2 = input("Enter second list elements: ").split()
difference = [x for x in list1 if x not in list2]
print("Elements in first list but not in second:", difference)Output
Enter first list elements: 1 2 3 4 Enter second list elements: 3 4 Elements in first list but not in second: ['1', '2']
Understanding List Difference
We iterate over the first list and keep only those elements that do not appear in the second list.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.