What if one of the lists is empty?


#1

Question

In this code challenge, what if one of the lists is empty?

Answer

This is optional, as the test cases of the challenge will always include at least one non-empty list. However, you can try to implement this as a further challenge.

There are two possibilities dealing with empty lists being passed to this function.

The first possibility is that only one of the lists is empty, and the other is non-empty. In this case, it would work similarly with two non-empty lists, since we only want to obtain an element from the larger list. In this case, the non-empty list must be the larger one, and we can simply obtain the last element from it.

The other possibility is that both lists are empty. In this case, since both are empty, we would try to return the last element of the first list. However, since there are no elements, there would be an error if you tried to obtain the last index of the list. So, to account for this specific case, consider adding some if statement that will check when both lists are empty, and print out or return some error message.