Why do we only need the range for one list?

#Write your function here
def same_values(lst1,lst2):
  lst3 = []
  for index in range(len(lst1)):
    if lst1[index] == lst2[index]:
  return lst3

#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

Ok so Im quite confused here. Why do we only have to say “for index in range(len(lst1))”. How come we are not also looking at the range for the second list. Can someone explain to me what the loop is actually doing?

1 Like

given the lists are of the same length, the indices for both lists are the same. Range just produces a list, this list just contains integers. These integers just so happen to match the indexes we need, so we can use these integers from the range list, to lookup values by index in both lst1 and lst2


@stetim94, I"m have a hard time following your explanation. Can you elaborate a bit more on:

“These integers just so happen to match the indexes we need, so we can use these integers from the range list, to lookup values by index in both lst1 and lst2”.

Thanks in advance.

Why do you struggle? You could start by printing index to see the values. Maybe that clarifies something?

remember that we can access an element by index:


which will give us the value positioned at that index.

By using a loop, we can get all indices to lookup the values in the list(s) we want

I guess I am not understanding where/how the “for index in range(len(lst1)):” loop is connected to (iterating through) lst2.

for index in range(len(lst1)) will give 0 in the first iteration of the loop, so then we can use index:

lst2[index] # index being 0

to access the first element (at index/position zero) of any list we want.


We need to be able to assume both lists are of the same length else we run the risk of an IndexError when attempting to access an element that isn’t there. When they are the same size, then their elements correspond by their index.

Later on we will cover some iterators of which a zip object is one.

pairs = []
for a, b in zip(lst1, lst2):
    if a == b: pairs.append(a)
return pairs

Note that the pairs list contains values, not indices.

1 Like

If we have two lists with corresponding elements at corresponding indexes, we can iterate through the indexes of either list and use it to access elements from both lists.

ind minerals sources
0 Diamond Canada
1 Calcite Brazil
2 Garnet Adirondacks
3 Anhydrite Mexico

We can do this:

minerals = ["Diamond", "Calcite", "Garnet", "Anhydrite"]
sources = ["Canada", "Brazil", "the Adirondacks", "Mexico"]
for ind in range(len(minerals)):
  print(f"{ind}. {minerals[ind]} can be found in {sources[ind]}.")

We could have used this as the header of the for loop instead, with the same result:

for ind in range(len(sources)):

Either way, this is the output:

0. Diamond can be found in Canada.
1. Calcite can be found in Brazil.
2. Garnet can be found in the Adirondacks.
3. Anhydrite can be found in Mexico.
1 Like

Got it! Reviewing @gearhead1309 code, I now understand that “index” is the connector.

This is slightly beyond what I have learned so far. I’ve began to learn in two ways: how things function; and looking at objects as puzzle pieces of larger functioning apparatuses.

For instance, I see how pieces of your code fits and works, but I don’t understand every facet of it.
Onward and upward.

Thanks for this step by step illustration. It brings the code to life.
Question: print(f"…). I assume “f” is for formatting. If so, how does that work?
Thank you!

Codecademy Community:
Where can I find more practice with loops?

1 Like

See PEP 498 – Literal String Interpolation for lots of information about f-strings.

1 Like

Thank you, @appylpye!

1 Like