Home Codewars Solution Arraydiff
Post
Cancel

Codewars Solution Arraydiff

A solution to an Arraydif challenge on codewars (in python).

The Challenge

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. So array_diff([1,2,2,2,3],[2]) should leave this for list a: [1,3]

I came up with this:

1
2
3
4
5
6
7
8
9
def array_diff(a, b):
    for item in b:
        try:
            while True:
                a.remove(item)
        except ValueError:
            pass
    print(a)
    return(a)

On a positive note all I can say is: it’s A solution but not THE solution 😀 After checking the best practices,a list comprehension is so much better. You live, you learn:

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

Which is a very elegant way of saying, give me everything in list a that is not in list b. I read on a topic about writing code that this is mostly how things go. You first focus on solving the problem after which you end up with a version of working but inefficient code. The next next step should be, to go through the code and refactor anything that improves readability and efficiency.

This post is licensed under CC BY 4.0 by the author.