Code Wars Python Challenge Walk Through - Array.diff

in #codewithmarky8 months ago

Here we are presented with another more difficult 6 kyu challenge which looks harder than it really is.

The challenge is to accept two lists and filter all items out of the first list if they exist in the second list.

As always, we want to break the problem into smaller problems.

  • Iterate list of values
  • Remove values
  • Return updated list

There isn't much to this challenge in terms of small problems. The initial approach I take is not the most pythonic way of doing things. I will explain that near the end.

The first thing we want to do is to iterate the list of values provided.

def array_diff(a, b): 
    for item in a:

This code will iterate each value in the first list. We can then check if the value is in the second list and act accordingly.

def array_diff(a, b):   
    for item in a:
        if item in b:
            # update list

This code simply checks if the item we stop at in the for loop is in the 2nd list provided. If the item is, we skip processing and move on. If it doesn't exist, we need to do something. You could have also done something if the item was in the list and skipped if it wasn't.

The reason I am doing it this way is because we are going to create a new list, and add the item to the list if it does not exist. Let's do that now.

def array_diff(a, b):
    output = []
    for item in a:
        if item in b:
    return output

I took the liberty of also returning the final list in this step as it is pretty straight forward.

Let's test our answer and see how we do.

We are getting all greens, so let's submit so we can run a full set of tests.

Still all good, looks like we have a viable solution, but is that solution the best way to do it?


Here you can see from the top answers our solution works but it is a lot more work than the other solutions. The reason for this is the popular solutions use what is called a list comprehension, one of Python's most powerful features. There are a few other unique ways you could have solved this problem as well but list comprehension is by far the most Pythonic.

A list comprehension is Python syntactical sugar, meaning under the hood it is essentially a for loop, but is available to allow you to write cleaner and less verbose code. Whenever you start iterating through a list or a string, consider using a list comprehension instead. I will do a future Python Tips Series post on List Comprehensions.

This brings me to another point I would like to make. You rarely will write optimized code the first time around. You will generally focus on solving the problem, and this process generally results in verbose repetitive code as you make attempts at solving the problem without side effects.

Once you have a working solution, you will want to go through and refactor that code to remove code smells and inefficiencies. During this refactoring, you will optimize for readability and performance.

Securely chat with me on Keybase

Why you should vote me as witness


Good job brother

Yet I voted you as my Hive trusted witness and the best way to pay back for that was to keep downvoting my post with automatic downvote. Wow. You are truly a great Hive witness among others in the top 30 witnesses. Please stop downvoting my post. Thank you sir.

I was told to post moderately,yet all Hive-Twitter Engagement are gone on Hive platform, as if that was not enough. I kept quiet and now you kept on downvoting those ones I posted again. Is this what Hive blockchain is all about? Please stop the downvote trail, and yet you never upvoted me once at all since I joined Steemit 2years ago and now Hive, yet you kept on holding back rewards. Too bad for your administration. Yet I looked up to you as my mentor. See what my so called mentorship is all about.