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

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.

10 Likes

we may also cyclically loop through the min_lengh_list the max_lengh_list’ number of times:

def same_values(lst1, lst2):
  list = []
  min_lengh = min(len(lst1), len(lst2))
  max_lengh = max(len(lst1), len(lst2))
  
  for i in range(max_lengh):
    if lst1[i % min_lengh] == lst2[i % min_lengh]:
      list.append(i % min_lengh)
  return list

lst1 = [1,2,1,2,1,2,7,8]
lst2 = [1,2]

print(same_values(lst1, lst2))

returns: [0, 1, 0, 1, 0, 1, 0, 1]

4 Likes

Although I’m not clear exactly what question it answers, it is an interesting example of the use of mod to reconcile two lists of different length,

4 Likes

Is there a way to assign a temp variable to a list without calling a loop?
For example:

def same_values(lst1, lst2):
  lst3 = []
  for index1 in lst1:
    if index1 == index2:
      lst3.append(lst1.index(index1))
  return lst3

Gives a NameError, that index2 has not been defined.
But if I define it through a loop, I mess up the function

def same_values(lst1, lst2):
  lst3 = []
  for index1 in lst1:
    for index2 in lst2:
      if index1 == index2:
        lst3.append(lst1.index(index1))
  return lst3

When running the following print for the exercise

print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

This returns

[0, 0, 2, 3, 3]

Due to the extra loop, instead of the intended

[0, 2, 3]

So is there a way to define index2 for lst2 without calling an extra loop?

You were closer the first time. You don’t need the double loop. You just need to remember, there are list indexes, and there are list elements. They are quite different.

my_lst = [5,4,3,2]

elements  5  4  3  2
indexes   0  1  2  3

for element in my_list: calls the elements

for element in my_lst:
    print(element)

Output:

5
4
3
2

for index in range(len(my_list): calls the indexes, which you can then use to retrieve the elements:

for idx in range(len(my_lst)):
    print(my_lst[idx])

Output:

5
4
3
2

And, if there is a second (“parallel”) list, the same index can be used for both.

my_lst = [5,4,3,2]
your_lst = ['a','b','c','d']


for idx in range(len(my_lst)):
    print(my_lst[idx], your_lst[idx])

Output:

5  a
4  b
3  c
2  d

Of course, you don’t have to print() the lists, you could do other stuff!!

1 Like

Thanks, I understand the difference between the elements and the indexes, but didn’t realize using the range(len(lst)) method called indexes instead of elements, makes a lot of sense.

However, I do not understand why the following is not passing this exercise:

def same_values(lst1, lst2):
  lst3 = []
  for index in range(len(lst1)):
    if lst1[index] == lst2[index]:
      lst3.append(lst1.index(lst1[index]))
  return lst3

It seems like the

print(same_values([5, 1, -10, 3, 3, 1], [5, 10, -10, 3, 5, 1]) )

is printing

[0, 2, 3, 1]

and I can’t for the life of me understand how it is turning index 5 into a 1, while the previous comparisons returned the correct value.

Edit: somehow

lst3.append(lst1.index(lst1[index]))

was the culprit, and just reducing it to lst3.append(index) fixed it. Still don’t understand how that caused this issue, though.

You are confusing the list.index() function with lst[idx]. You only need the latter.

You can pretty much forget lst.index() ; all you need for these beginning exercises is for-in, range(), and lst[idx] (and slicing if you have gotten that far yet.) Keep it simple.

Hey guys, I’m having trouble understanding why my function will only return [0, 2, 3] when inputting same_values([5, 1, -10, 3, 3, 1], [5, 10, -10, 3, 5, 1]). Technically it should return [0, 2, 3, 5]

Currently this is the code I have running.

def same_values(lst1, lst2):
  equal = []
  for i in range(0, len(lst1) - 1):
    if lst1[i] == lst2[i]:
      equal.append(i)
  return equal

From my understanding, this should run through the list index all the way to the end, but for some reason it’s stopping abruptly at lst1[3] or lst2[3]

Why do you have range(len(x)-1)? The “strange” way that that second range() parameter works is designed specifically to work with len().

Say lst_x contains 6 individual numbers, the len(lst_x) would return 6, correct?

However, the index in lst_x would run from 0 - 5, meaning that if we call lst_x[6], wouldn’t we get an error?

This is my understanding.

range() returns a sequence of integers up to, but not including, the value assigned to the second parameter.

Yes that’s true! I forgot that the range function stops before the second parameter!

Thank you.

1 Like

In this code challenge, i tried to answer in the way below. But i received that this codes can not be run, may someone explain for me about this case?

def same_values(lst1, lst2):
index =
k = 0
while k < len(lst1):
if lst1[k] == lst2[k]:
index.append(k)
else:
continue
k = k + 1
return index
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

how did you come up with this??? could you explain why you used the modulo operator?

thanks a lot!!!

1 Like

hi. Modulo allows us to loop over bigger list by numbers of smaller list:
let’s assume, that we have 2 lists:
1st list has 7 elements (max_lengh)
2nd list has 3 elements (min_lengh)

our purpose - to achieve:
big list - small list:
1 - 1
2 - 2
3 - 3
4 - 1
5 - 2
6 - 3
7 - 1

we apply modulo in the loop circle from 0 to 7 (exclusive)
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0

we use results as indeces and append numbers with these indeces to the list.

1 Like

here is my solution for the main task

def same_values(lst1, lst2):  
  return [i for i in range(len(lst1)) if lst1[i] == lst2[i] ]

print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))
#[0, 2, 3]
4 Likes

here is my solution
def same_values(lst1, lst2):

nums =

for num in range(len(lst1)):

for num in range(len(lst2)):

  if lst1[num] == lst2[num]:

    nums.append(num)

return nums

Goes off what you did

#Write your function here
def same_values(lst1, lst2):
    if len(lst1) > len(lst2):
        return [i for i in range(len(lst2)) if lst2[i] == lst1[i]]
    elif len(lst1) < len(lst2):
        return [i for i in range(len(lst1)) if lst2[i] == lst1[i]]
    else:
      return [i for i in range(len(lst1)) if lst1[i] == lst2[i]]

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

What about the code, which think also should be the answer but the output is None:

def same_values(lst1,lst2):
num =
for item1 in lst1:
for item2 in lst2:
if (item1 == item2):
num.append(item1)

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

Why this code do not work

for lists of unequal lengths you can do like this…

def same_values(lst1, lst2):
  k=list()
  for i in range(min(len(lst1), len(lst2))):
    if lst1[i]==lst2[i]:
      k.append(i)
  return k
3 Likes