Weekly Coding Challenge: Array.diff using List Comprehension and some pros/cons of using it – written in Python

Posted by

2 min read ~ Hello readers! It’s really important for me to continue my practice and learning. Every week, I block off 30 minutes, set a timer and choose a challenge from codewars. This helps me keep my skills sharp, see how other devs all over the planet would solve these problems, and take some leisure time to problem solve and be creative. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. Today, the problem set focuses on Python.

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

array_diff([1,2],[1]) == [2]

If a value is present in b, all of its occurrences must be removed from the other:

array_diff([1,2,2,2,3],[2]) == [1,3]

The following is the solution:

def array_diff(a, b):
    return [x for x in a if x not in b]

I love the solution above because it is so simple and clean. For those who are unfamiliar with List Comprehension in python, it allows us to shorten the syntax of an algorithm to a single line of code when creating new lists to return or continue on in the procedure of the application. It can be really powerful to keep things clean and efficient in our code. In the above requirement to compare the list and remove the duplicates found in each, list comprehension comes in handy to evaluate that problem in a single line. However, lets look at what this would be written in a more traditional way:

def array_diff(a, b):
    new_lst = list()
    for x in a:
        if x not in b:
    return new_lst

As you can see above, we are simply initializing a new list, looping over the first list, and comparing the values with the second list to qualify their existence in both and returning the values that remain in our variable called new_lst. Now, if we take this example and look at the list comprehension used above to solve the same problem, we start to see some similarities:

x is the qualified value that we are appending to the new_list

for x in a is the actual element of the array that is being iterated over

if x not in b is the qualifying condition that we placed within our for loop in the bottom example

[ ] is the new_list array that we are appending the x value to once it has been qualified

Now, where can this be troublesome? Watch out for nested comprehensions. While this can be a great way to use list comprehension to be more efficient, this can also make your code virtually unreadable and hard to follow given the complexities of data types and conditional requirements that your problem might be solved with.

I hope that this was a useful and simple guide for list comprehension and I had a lot of fun writing this out. If you have any questions or feedback, please dont hesitate to reach out.

All solutions are also now on my GitHub profile if you want to check out all of the coding challenges I’ve completed so far!

All credit to problem sets goes to codewars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s