Python Challenge : What code is better?

Hey, I’m doing the career path for “Data Scientist”, and currently loving python.

I’ve got to the list challenge section:
https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/articles/python-code-challenges-lists

What I like doing is comparing my code with what Codecademy gives if you’re struggling for the solution.

Challenge 1: Append Sum

My code:

def append_sum(lst = []):
    lst = lst
    for i in range(3):
        a, b = lst[-1:-3:-1]
        lst.append(a + b)
    return lst

CodeCademy:

def append_sum(last):

  lst.append(lst[-1] + lst[-2])
  lst.append(lst[-1] + lst[-2])
  lst.append(lst[-1] + lst[-2])

  return lst

I’m curious what is the better code and thought this would be a good chance to communicate with the Codecademy community to get some positive criticism for improving.

Albeit defining which code is better is generally tough, in this case, I am secure to say that your code is better since it’s more suitable for automation. The Codecademy code is okay only because there are 3 iterations; however, the more iterations, the worse the Codecademy code gets. Your code does not suffer this issue.

In the fourth line of your code, you could have replaced

a, b = lst[-1:-3:-1]

with

a, b = lst[-2::]

but it is just a matter of taste hahaha.

1 Like

johnpierucci is right, the loop is absolutely better. Your code follows DRY (don’t repeat yourself), codecademy solution doesn’t. That codecademy doesn’t follow DRY could be because loops have not yet been introduced?

I also agree with johnpierucci on the fact that lst[-1:-3:-1] is difficult to read. Unclear in one look what this code is suppose to do. Couple of things you can consider:

  1. replace it with something more readable.
  2. put a comment
  3. extract the logic into a separate function and give the function a logical name. Then the function name tells you what is happening.

this line:

lst = lst

either needs fixing or needs to be removed. I understand why people think this will make a copy of the list, but that is not the case. You now have another variable pointing to the same list in memory

mutable default arguments are also not recommended, as explained here:

Common Gotchas — The Hitchhiker's Guide to Python

2 Likes

Thanks, johnpierucci , and stetim94 for your comments. I can totally see where you two are coming from for readability concerning the alterations you’ve suggested.

Thank you for the resource as well stetim94 . I have a few bad habits and see the redundancy in the following code since you’ve pointed it out:

lst = lst

Have read through the given article, and can see where I can improve on certain areas.

2 Likes

Hey I’m digging python so far too. So I thought I would show you my loop solution for this.

def append_sum(lst):
  length = len(lst)
  while len(lst) <= (length + 2):
   lst.append(lst[-1] + lst[-2])
  return(lst)

It took me couple of tries to get the syntax of my variables correct, allowing me to compare the current iteration of ‘lst’ to the next using the variable ‘length’. But I was happy when this worked.

1 Like