Haloo people! How long has it been since I’ve done a blog post? Welp, I’ve finished my long break and now it’s time to get back to work. Today, I’ll be writing a python program that prints the elements of one list (a) that are not in the other list (b) as a list.

My conditions are:

  1. If the lists have the same elements, it will print an empty list.
  2. If the first list (a) is an empty list, it’ll print an empty list.

An example -

List a
[1, 2, 3, 4]

List b 
[1, 2]

Output 
[3, 4]

1. Making a List

First, I’ll be making 2 lists. List a and list b.

a = [1, 2, 3, 4]
b = [1, 2]

And then an empty list for the output.

output = []

2. For loops

Now it’s time to make a for-loop.

for i in a:
    if i not in b:
        output.append(i)

3. Adding the final piece

Now to add the final piece. The function print. Or else, you won’t see ze final piece.

print(output)

4. Putting them together

When putting everything together, you’ll get:

Input -

a = [1, 2, 3, 4]
b = [1,2]
output = []

for i in a:
    if i not in b:
        output.append(i)

print(output)

So then my output will be:

[3, 4]

So there you go!