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.
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.
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
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]
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]))
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.
#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]