How can I make this function work for lists of unequal length?


#1

Question

In the context of this code challenge, how can I make this function work for lists of unequal length?

Answer

To make this function work for lists of unequal length, we would just need to compare up to the last index of the smaller list.

For example, if we had the following lists,

list1 = [1, 2, 3, 4]
list2 = [1, 2, 3, 4, 5, 6, 7]

we can see that there would be no need to compare the values after index 3, because that is the last index of the first list.

So, to implement this function to work for uneven list lengths, you would first find which list is smaller. Then, you can perform the comparisons over the indices in the smaller list only, since we know the larger list will include those indices as well. For instance, for the above example, we would only need to compare values at indices 0, 1, 2, 3.