FAQ: Code Challenge: Loops - Same Values

#22

I’m sorry - now I see that my description of a problem is very misleading! I meant that I don’t know how to get rid of double 3 in my output. Of course, after running a code, I get just one line of text which is [0, 2, 3, 3] .

#23

now i understand.

you understand that you only need to include values which are present in both list at the same index?

the problem with your if condition is that it doesn’t care about indexes.

the last item of lst1 (3) is also included, given it will equal the second to last item of lst2.

keep in mind that .index() method gives the first occurrence in the list

1 Like
#24

When using “range(len(lst1))” it will give an index range for the length of the first list. These numbers are not using the values in that particular index for that list. This means that by having this number set you can compare both lists with the index number, further finding values at that given index. Having another list length would be considered inefficient, as this is a comparison of values with the same indexes.

If you still don’t quite understand what I mean I will supply my code with annotations

#25

is this the best way of doing it?
if not how can i improve it?

this returns the indices which match but before all that it checks which list is smaller and uses that as the range

def same_values(lst1,lst2):
  index = []
  length = range(len(lst1))
  length2 = range(len(lst2))
  
  
  if len(lst1) <= len(lst2):
    for i in length:
      if lst1[i] == lst2[i]:
        index.append(i)
    return index
  else:
    for i in length2:
      if lst1[i] == lst2[i]:
        index.append(i)
    return index
      
#26

zipping the two lists resolves the uneven length concern by truncating the longer of the two lists. Values are paired up by their indices. 0:0, 1:1, 2:2, &c. Mind, the instructions do state two lists of numbers of equal size.

With a zipped list we only iterate a single object. Using enumerate we can track the index.

for i, x in enumerate(zip(lst1, lst2)):

x is a tuple, so we can unpack it…

    a, b = x
def same_values(lst1, lst2):
  y = []
  for i, x in enumerate(zip(lst1, lst2)):
    a, b = x
    if a == b:
      y.append(i)
  return y

The more naive approach is to keep track of the index with a count variable.

    count = 0
    y = []
    for x in zip(lst1, lst2):
        a, b = x
        if a == b:
            y.append{count)
        count += 1
    return y

We will most certainly come up with other naive approaches if one sticks with the problem awhile. Play it out and see how many you can come up with.

1 Like
#27

here’s my solution

def same_values(lst1, lst2):
  same = []
  i = 0
  
  if len(lst1) == len(lst2):
    for num in lst1:
      if lst1[i] == lst2[i]:
        same.append(i)
      i = i + 1
    
    return same
#28

@bitplayer30925 Man. Please stop posting the answers

#29

Sorry - didn’t realize that’s a faux pas. Just wanted to document one method of completion, since I like looking at how others solve.

I can obscure the solutions.

#30

Yes, that would be good idea. Please do so.

#31

Hello i believe i have solved the code chalanges (loops) same values. But i can’t pass the subject
here is my code

#Write your function here
def same_val(lst1, lst2, res=[]) :
  fa = lst1[0]
  fb = lst2[0]
  ra = lst1[1:]
  rb = lst2[1:]
  r = []
  if fa == fb :
    r.append(fa)
  if ra == [] :
    return res
  return same_val(ra,rb, res+r)

def same_values(lst1, lst2) : 
  res = same_val(lst1,lst2)
  result = []
  for n in res :
    result.append(lst1.index(n))
  return result
#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

I’m using helper function same_val to recursively get the same values and then get the indicies in the same_values function. is this a bug or what? thanks

#32

Hi all,

I figured out the for-loop solution but was trying to find and alternative using list comprehension… and failed. What’s wrong with this / what is the way? (error was list out of range). My sense is that the index object is looking for “i” rather than what I intend which is the first element of lst1 if it is the same as the first element of lst2.

svi = [lst1.index(i) for i in lst1 if lst1[i] == list2[i]]

Thanks for any help.

#33

your variable i contains value from the list, so trying to access by index (lst1[i]) will throw an index error when there is a value in the list which is higher then the highest index (which is quite common)

i wouldn’t use the .index() method, the index method always gives the first match, which is problematic if one value occurs multiple times in a list

#34

Thanks. Is there a way to use list comprehension to solve?

#35

yes, there is. You could use range() to get the indexes/indices. That should help