List Challenge

Hello all!
Help me somebody to understand why my code returns “None” in this challenge:

def append_size(lst):
new_lst = lst.append(len(lst))
return new_lst

print(append_size([23, 42, 108]))

Look at where you got it from.

if you do

a = 2 + 3

then you’d get 5 written to screen. where does 5 come from? you’d look at the definition of a, which is obtained by giving the values 2 and 3 to +, and the result you got back is what you assigned a to

same deal with your code. you have some series of operations where you can trace what the involved values are, all you need to do is observe

This could be because of the variable (new_list). Get rid of the variable. See below:

def append_size(lst):
    return lst

print(append_size([23, 42, 108]))

It will return: [23, 42, 108, 3]
You understand now?

the variable itself isn’t doing anything though, the problem comes from the operations carried out - paying heed to what inputs are fed in and what outputs are obtained for the operations

removing the variable while otherwise keeping it equivalent (same operations) would be:

def append_size(lst):
    return lst.append(len(lst))

print(append_size([23, 42, 108]))

which would have the same result as the original code, a return value of None

1 Like

In your case a variable “a” will return 5. But I still can’t get why it didn’t in my case.
I assigned a result of calculation to a new variable and trying to return it, but returns None.
I know if I just remove variable the code will work, but I am trying to understand the language here.

Removing the variable makes no difference. You’re probably making another change as well.

If you look at where you take the result from, you’ll find where None comes from. And if you wonder why you are getting None from that operation, then, what does that operation promise to do, does it promise to return something other than None? It has some particular behaviour and you can leverage that, but if you use that operation while ignoring how it communicates then you’re pretty likely to misinterpret it.

So there are two parts to this. One is looking at where you got it from, it doesn’t appear out of nowhere, you are getting it from some operation and all you need to do is to follow the assignments backwards to the source.
The other is considering how that operation communicates its result.

I expect it to append the length of a list to a “new_lst”, then return that new_lst.
If I just remove the variable “new_lst” and return “lst” I will get the expected result, it just doesn’t make any sense.
So I guess I can’t save that operation to a variable.

It makes no difference whether you use a variable. However, which value is it that you’re supposed to return?

def five():
    a = 7  # makes no difference whether this exists
    b = 5
    return b

Variables don’t DO anything, it’s your actions you need to pay attention to, not whether or not you use variables.

In particular, using a variable does not cause any value to change into something else.

You’ve got multiple values and you’re returning the wrong one. You have to consider which of the available values you use.

And if you expect some operation to behave a particular way, but it doesn’t, then you would probably want to find out what that operation promises to do.
Or, rather, for any operation that you don’t know what it does, you’d want to find out before using.
They have some particular behaviour, and you’ll have to adapt how you use them to how they behave. They won’t adapt to how you mean they should behave.

1 Like

It started making sense. Thank you for you patience!

1 Like