I'm supposed to create a function that compares two lists, finds the index at which they differ, and then return the list that has the lesser value at the index at which they start to differ
Here's what I have.
def function(foo):
i = 0
while a[i] == b[i] and i<min(len(a), len(b)):
i+=1
if a[i] < b[i]:
return a
else:
return b
You almost had it. You're getting an out of bound error because within your while loop, you're checking the indexes of both a and b AFTER you already incremented i.
With some minor tweaks you've basically got it.
Here is your way tweaked a bit:
l1=[1,2,3,4]
l2=[1,2,4]
def foo(a, b):
i = 0
while i < min(len(a), len(b)):
if a[i] < b[i]:
return a
if a[i] > b[i]:
return b
i+=1
print(foo(l1, l2))
Will print
[1,2,3,4]
Note... you should consider the case where l1=[1,2,3,4] and l2=[1,2,3]. What do you actually want to return? If we stick with your else statement then l2 (or b) will return. With my tweak it will return None.