Python List Issues - Always returning None

Hi All,

I’m having issue comprehending why my code is returning ‘None’ while the correct solution code is returning ‘5’
I’ve put both the solution, and my original code into the same IDE to compare them.

From comparing, I can see I definitely used more code than needed, but I still don’t understand why it does not work?

See the snippet below, the first is Codecademy solution, second is mine,

#Correct Solution
def larger_list(lst1, lst2):
if len(lst1) >= len(lst2):
return lst1[-1]
else:
return lst2[-1]

print(larger_list([4, 10, 2, 5], [-10, 2, 5, 10]))

#My Answer
def larger_list(lst1, lst2):
if len(lst1) > len(lst2):
print(‘List 1 has more elements’)
return lst1[-1]
elif len(lst1) < len(lst2):
print(‘List 2 has more elements’)
return ls2[-2]
else:
return print(“List 1 was chosen as both have the same amount of elements”)
return lst1[-1]

#Uncomment the line below when your function is done
print(larger_list([4, 10, 2, 5], [-10, 2, 5, 10]))

Lists challenges are the first of the course I found to that didn’t ‘click’ straight away.
Any help is appreciated!

Regards,
Chris

I’ve since learned that I had two 'return’s in a row.
Removing the return and simply using print fixed this.

It now looks like

else:
print(“List 1 was chosen as both have the same amount of elements”)
return lst1[-1]

Is it not possible to have two return’s in a row?

No, return hands back data to the caller and signals the function is done/completed.

Hi stetim94,

Yes I’m not sure why I put a return in front of the print statement in the first place!
I’ve since done ‘break’ in loops and I assume return works the same, adding a print statement after a break won’t show. Same as after a return because it as ended.

Thanks for your reply.

return and break do not do the same thing, the only thing they share is that code after these keywords might not be reached.