Learn

Now we need to build out our result list. When we’re merging our lists together, we’re creating ordered lists that combine the elements of two lists.

Instructions

1.

Since we’re going to be removing the contents of each list until they’re both depleted, let’s start with a while loop!

Create a loop that will continue iterating while both left and right have elements. When one of those two are empty we’ll want to move on.

2.

Now we do our comparison! Check if the first element (index 0, remember) of left is smaller than the first element of right.

3.

If left[0] is smaller than right[0], we want to add it to our result! Append left[0] to our result list.

Since we’ve added it to our results we’ll want to remove it from left. Use left.pop() to remove the first element from the left list.

4.

If left[0] is larger than right[0], we want to add right[0] to our result! Append right[0] to result and then pop it out of right.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?