FAQ: Code Challenge: Lists - Larger List

This community-built FAQ covers the “Larger List” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Larger List

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

why the else:
return lst2[-1]

Because the instructions are to make the code return the last item of lst2 (which you get with -1) in the case that the length of lst2 is higher than lst1.

1 Like

Why is the solution different when I use lst1[ -1: ] vs lst1 [ -1 ] ? The output prints [ 5 ] vs 5 which is what the program is looking for.

Slicing and indexing are not the same thing.

A slice returns a list; a list element accessed via indexing returns the element.

Oh, ok. Thank you for the explanation.

1 Like

hi,

this is my code for this exercise:

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

when i run this code, the function works, but if i replace “<” with “<<”, it doesn’t work anymore. just wanted to know why that is, and if someone can explain the difference in practice between < and << (i.e when do we use one over the other)

Since there are two scenarios in the instructions for which we return the last element of lst1, I used an “or” statement to return lst1[-1] if either scenario evaluated to True, otherwise, return the last element of lst2.

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

Please note that Python and Python3 (and most programming languages) have a build-in operator smaller or equal:<= and larger or equal:>=. They are used very often so it’s good to get well acquainted to these. It makes the code almost half the original length!!! :smiley:
You can replace your if statement to have just one condition as shown below:

 if len(lst1) >= len(lst2): 
       [executed code]

Goodluck with your studies! :slight_smile:

1 Like

Oh boy, I don’t know how I missed that haha. Need more coffee! Thanks!

1 Like