Loops and Lists


The Problem

Write a function called identical_indexes that takes two lists and returns a list containing the indexes into both lists where the values are identical. If there are no such values, the function should return an empty list. Be careful to not have an index out of bounds error.

Dissecting the Problem

This question wants to know if there are any indices in two lists that have the same value, where and indices is a spot in a list. For example:

Although these two lists have values in common (1, 2, and 3), those values aren’t at the same indices or spots. In list1, 1, 2, and 3 occupy indices 1, 2, and 3 respectively, but in list2, 1, 2, and 3 occupy indices 0, 1, and 2. In this case, identical_indexes would return an empty list, [].

Another example is this case:

The values that are the same and in the same spots across both lists are 4 and 5. 4 and 5 are located at indices 1 and 2, so identical_indexes should return the list [1, 2].

Attempting the Problem

The function has to be called identical_indexes, we know it takes two lists, and we know it returns a list, so before, even thinking about solving the problem, we can set up our function:

Next, in order to find indexes with the same value, we have to go through all the indexes. Python provides a lot of ways to iterate through a list, but when you care about the indexes, you do some variation of for i in range(someInteger):. In this case we want, to go through an entire list, so we have to go through the length of the list.

Now that we’ve decided how to go through our list, we need to decide what to do inside of our loop. Based on the problem, we know that we need to find the indexes that have the same value, so we know we need to compare the values at each indexes.

But what do we do when the values are the same? We need to add this to our list, and in python there a few built-in functions to do this. One of the most common is someList.append(someValue), which takes whatever someList is and adds someValue to the end of that list as the new last value. Since we want to return the indexes (not the value), we need to append the index to the list we are going to return.

Is the Function Correct?

Tracing through the function, let’s test if it returns correctly, for the following values of

After each loop through:
The list that will be returned is empty, because 0 and 3 are different values.

  1. The list is now [ 1 ], because 4 and 4 have the same value

  2. The list is now [ 1, 2 ], because 5 and 5 have the same value

  3. The list is still [ 1, 2 ], because 3 and 0 have different values

What about the following example:

After each loop through the values are the following:

  1. The list that will be returned is [ 0 ], because 1 and 1 have the same values

    • Python would raise an indexError on the line with the comparison statement

  2. The program will crash during the second run through, because list2 has no index 1

  3. --------------- The third run through did not occur because the program crashed

This is a problem, because we want out program to work as long as the inputs are valid. Looking at the test case that failed, it crashed because list1 is longer than list2, so we need to figure out a way to check if the index is in both lists.

Adding a condition to the if statement to check if the index is less than the length of the list can prevent the index statement, but only if it is first. If the if statement looks like this:

In this case, you are indexing into the list, before you check if the index is valid, which means that the program might crash anyways. The reason why

is correct when the reverse isn’t, is because if the first statement evaluates false, the second statement isn’t checked. What this means is that if the index i isn’t is too big for the list2, you won’t try to access index i of list2.


The final solution is: