Larger List practice challenge

Hello! I’m having a hard time figuring out what this program wants me to do. I don’t understand where I’m supposed to get the lists.

https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-lists

  1. Larger List

"Write a function named larger_list that has two parameters named lst1 and lst2 .

The function should return the last element of the list that contains more elements. If both lists are the same size, then return the last element of lst1 ."

I don’t know if I was on the right track, but what I did first was create a variable called lst1_range = range(0,20,2). I feel like this isn’t what it wants.

The lesson gives this:

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

here, we expect the function to return 5, the list are of equal lengths, so we should return the last element of the first list

if we had:

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

you should return 3. There are more elements in the second list, so you should return the last element of the larger list

So the lists it wants me to work with are the two in the print function? Would I setup the parameters as empty lists?

I setup the function as:

def larger_list(lst1 = [ empty ], lst2 = [ empty ]):

The two lists in the print function are just for example. You don’t have to use lst1 = [], you can just use the parameters without default assignments.

I get it now. So what I was doing was using lst.count() instead of len(). That’s why I couldn’t figure out why it kept saying it should have returned 5, instead it returned 10.

the .count() method serves a very different purpose:

1 Like

The idea is to return the last element of the list with the greatest length. If they have the same length, you should return the last element of the first list.

Right. I ended up seeing what the solution was after I put mine in. Makes sense now that I see how it should look:

#Write your function here

def larger_list(lst1, lst2):

if len(lst1) >= len(lst2):

return lst1[-1]

else:

return lst2[-1]

#Uncomment the line below when your function is done

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

It returned 5

1 Like