How can I get the sum of all values in a list?

As mentioned this is not a comprehension since there is no iterator.

[x for x in object if x % 2 == 0]

In the above, x is the iteration variable, for..in is the iterator, object is the iterable, and if is the conditional that determines if x is appended to the output list.

return lst1 if sum(lst1) >= sum(lst2) else lst2

The above is a conditional expression, only. It is often compared to the ternary expression found in C, Java, and JavaScript, though not available in Python…

sum(lst1) >= sum(lst2) ? lst1 : lst2;

Part of my issues was that I had become too used to seeing: Create an empty list. So I struggled for a while bc part of me was treating the new variables (sum1 & sum2) as lists (even though I wrote them as variables and not lists:crazy_face:) as I was planning out the method I’d use in my code. Once I realized they were storing a single total number the process made more sense.

Maybe it teaches you how to figure out a problem without complete help. This seems to be an important skill to have, and so they wish to present a more realistic training experience.

2 Likes

It’s not exactly following the rules since there is no loop, but my solution was:

def larger_sum(lst1, lst2):
  return lst1 if sum(lst1) >= sum(lst2) else lst2

Under what circumstances would Codecademy’s solution to this exercise be preferred to just using an if/else statement?

My solution:

[spoiler]
def larger_sum(lst1, lst2):
 if sum(lst1) >= sum(lst2):
  return lst1
 else:
  return lst2
[/spoiler]

The point is probably to reason about the actions needed to obtain an answer, not so much what’s preferred.
Preferred would probably be to use the built-in max function, there’s no need to write any new code since this already exists.

Once we are allowed to use built-in functions, the code becomes much more concise and easier to understand!

def larger_sum(L1, L2):
  return L1 if sum(L1) >= sum(L2) else L2

■■■■… this is everything I have been trying to articulate in my code, but failed to be able to make the necessary logical leap. Thanks for sharing your solution.

max has an argument that lets you say how to compare things, so you can say: max by sum

you can also use itertools.partial to supply this argument in advance:

larger_sum = partial(max, key=sum)

this is just one of many examples where you can combine some concept of iteration (max) with an idea of what to do with each value (sum), where both those concepts are implemented as functions, and the combining concept (partial) is also one. you can compute new useful functions using functions as both the operators and the operands.

1 Like

My answer for this code lesson

def larger_sum(lst1,lst2):
  sum1 = sum(lst1)
  sum2 = sum(lst2)
  if (sum1 > sum2):
    return lst1
  elif (sum1 < sum2):
    return lst2
  elif(sum1 == sum2):
    return lst1
  else:
    return 'You have error in your code!'

I have misinterpreted many of the instructions in this course. My worst one was find the odd indices. I thought the instructions called to find the odd numbers with in list. I spent a few hours messing around with a program that would do this and eventually i was convinced they screwed up what would give a correct answer. I finally got answer and spent another 2 hrs trying to relate how the given answer fulfilled the request to find the odd numbers in the list. (it gave the values in the odd indices)

in the end i realized that I was wrong and realized its what comes with the learning experience and soon to be job.

Embrace the frustration

2 Likes

could someone explain why we don’t need to use here else?

else: return lst2

def larger_sum(lst1, lst2):
  sum1 = sum(lst1)
  sum2 = sum(lst2)
  if sum1 >= sum2:
    return lst1
  return lst2

else is the default case. It is the branch that is followed when all other cases fail.

Above there is only one condition, which if met will channel control flow through that branch. There is no other state other than its opposite. The last line of the function will not be reached unless the condition fails. When it is reached, we know that lst2 is greater.

The key is the use of return, which terminates the function and passes control back to the caller.

1 Like

I guess that with expirience my code will be more simple and efficient, i’m solving the excersices but my code always seems longer than the ones in here.

def larger_sum(lst1, lst2):
  total_list1 = 0
  total_list2 = 0  
  for sum1 in lst1:
    total_list1 += sum1
  for sum2 in lst2:
    total_list2 += sum2
  if total_list1 > total_list2:
    return lst1
  elif total_list1 < total_list2:
    return lst2
  else:
    return lst1

If I am correct it is because the second return that calls “lst2” is outside the first return part of the function which gives it an innate “else” value.

Hey everyone! I used this code:

def larger_sum(list1,list2):
sum1 = sum(list1)
sum2 = sum(list2)
if sum1 > sum2:
return list1
elif sum2 > sum1:
return list2

for the exercise Code Challenge: Loops, Larger Sum. It was a different way of doing it than the actual solution, but it gave me the correct answer. It returned the same response as the solution. However, it wouldn’t accept it. Does anyone know why?

What is the expected return if both sums are the same? Currently it is, None. Could that be the reason it is not passing?

I haven’t encountered any exercises that ask you to use anything that wasn’t previously taught, including this one.

It would return lst1, would it not?

Yes, I was trying to guide the user towards returning something different, since the exercise asks you to return the list with the greater sum. If the sums were the same, neither list should be returned (unless the instructions specify so, I can’t access the lesson without PRO).