Append sum list function

Hey so i created this function, although it is correct wanted to get some feedback on it so I can learn to keep it dry next time i see a similar problem

Without giving complete answers something like list slicing and a loop would save you a bit of repetition. Recursion might save you a touch more though it’s less often used in Python (it’s a bit slow and could potentially reduce readability though it has valid uses).

1 Like

This is fibonacci sequence. You can google it to learn more about this pattern. What I suggest here is using loops or maybe recursion. As I can see here, you might not have reached to any of these modules so this code is perfectly fine for your level. But think that what is you had another parameters, integer, to how many times you have to repeat the process. That’s when these things come handy.
Here is the loops solution code for this same problem. You can read this now or after you are completed with loops.

# Problem 1: without any extra parameters
# Constraint: List must be of size equal to or greater than 2
def append_sum(lst):
    for i in range(3):
        lst.append(lst[-1] + lst[-2])
    return lst

# Problem 2: With additional integer parameter
# No Constraints
def append_sum(lst, num):
    while num >= 0:
        if len(lst) < 2:
            lst.append(1)
            continue
        lst.append(lst[-1], lst[-2])
        num -= 1
    return lst

# Swapping is also another option but here we had a list t which we needed to append some value.

If I did anything wrong in code correct me guys . I would love suggestions to improve!
:upside_down_face: :upside_down_face:

1 Like

bruh this looks fire

1 Like