3. Larger List

In the 3. Larger List exercise, how would we go about solving the question using the max() function applied over the len() of the list?

The question states

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 .

https://www.codecademy.com/paths/data-analyst/tracks/dacp-python-fundamentals/modules/dscp-python-lists/articles/python-code-challenges-lists

This is what I’ve tried

def larger_list(lst1, lst2):
  if len(lst1) == len(lst2):
    return lst1[-1]
  else:
    x = max(len(lst1), len(lst2))
    return 

I was thinking of returning the name of the list corresponding to the list with the most elements, but I don’t know the name of the function for that

How useful is that information?

I know the solution but was wondering if it can be solved this way. My thought process was to return the name of the list with the most elements when comparing 2 or more lists using the max function, and then call the last element of that list

What if the problem extends to 10 lists instead of 2? We can’t just keep using if statements

That much I can reason, but how do we know which list it is? x is just a length.

I would say, with x, return the name of the list with length x? And then with that list, call the last element.

Which list would that be? What if there are multiple lists with that length and it just happens to be the max of all the lists?

First, solve this for two lists.

If there are multiple lists with that length, I would say return the 1st list of length x

For two lists, I’m having trouble because I don’t know the syntax of returning the name of the list with length x

Because you are focusing on the lengths and not the lists. We cannot lose sight of them in this process. It’s important we keep our hooks in.

Is the length of lst2 greater than the length of lst1? If so, return it. Otherwise return lst1.

Yes, this is the solution I got. I understand it

def larger_list(lst1, lst2):
  if len(lst1) <= len(lst2):
    return lst1[-1]
  else:
    return lst2[-1]

However, the question I asked is if this is doable using the max() function and I am stuck on that part.

Did that solution pass the lesson checker?

Yes. I checked that solution and matches with Codeacademy’s solution

One will by this point appreciate that max() is of marginal use in terms of tracking the objects. Where only two objects are involved it is pointless, tbh. We have to run the len() function on both objects, regardless.

The max function deserves further investigation which will prove useful in reaching the answer to your question. How do the docs describe it?