Why the output is None ? | Sololearn: Learn to code for FREE!

+2

Why the output is None ?

I am not sure why the output is none here ? lst=[].append(5) print(lst) I believe it is because of [] but I am not sure how would be the correct output?

11/19/2019 6:22:52 PM

THEGreatGatsby

21 Answers

New Answer

+12

The append method does not return a value at all. The [] object is not assigned to a variable, so its modification is lost forever, because you have no chance to access the object.

+8

List append does not return explicit value. Any function in python that does not return explicit value returns None implicitly, which is getting assigned to lst variable here which you are printing. You can try this, you will still get None lst = [1] lst = lst.append(5) print(lst) output = None

+4

It's a convention. for every methods that mutate values, None should be returned.. just like, sort(), add(), append(), e.t.c

+4

THEGreatGatsby method = function Everything in python is an object. This function returns 1. def func(): return 1 This function returns None def func(): print(1) All functions that doesn't have the return keyword would return None. I think append() is among

+4

You can try this, it will work: lst = [1] #lst = lst.append(5) # <--- don't use this lst.append(5) print(lst) #output = [1,5]

+4

Michael Which scenario is more correct in terms of what is happening. 1. The function returns an implicit None since there is no explicit return value or 2. The function does not return a value at all but since there is an assignment involved, the variable must be assigned something hence None is the best fit

+4

~ swim ~ I quote the official Python documentation: "The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.” (https://docs.python.org/3/library/stdtypes.html) The Null object “is returned by functions that don’t explicitly return a value. It supports no special operations. There is exactly one null object, named None (a built-in name).” (https://docs.python.org/3/library/stdtypes.html#the-null-object)

+4

Michael Thanks for the information. So it clearly returns implicit None. I asked since you said appends does not return any value at all so just had a bit of thought about what exactly is happening.

+3

Yes the result would be same as none of those methods return explicit values

+3

Michael "null" does not exists in python but people would say None == null In other programming language. If it returns nothing, it returns None. It can't return null Both link you shared above are explaining the same thing in a different way. Then note that the 'return None" can also be made deliberately a = lambda : None print(a) #None

+3

Mirielle🐶 [Inactive] You are right, but I have never argued that there is a “null” in Python. Nevertheless there is a “Null object” in the meaning of a design pattern, which is an established term. It is also the terminology in the official Python documentation. The Null object in Python is implemented via the class NoneType which has None as its sole instance.

+3

Vikas Jain Seriously ! The whole thread is about why the output is None. Then you are asking why the type of None is None? According to you what should be the type of None? Moreover you have already completed your python course !

+2

This method changed the list immediately in place and it returns none, this works with sequences that are not constants. For example, you can take a str and a list, and append and replace methods, the append list method will change the existing list without creating a new one, and the str method will return a new str with the changes

+1

but in our case we are appening value 5 ? so why not 5 for answe?

+1

do you mean that if append with value would return value in case lst[1]?

+1

Yes cos it's a convention. append(), sort(), add() etc are functions that returns None.

+1

Also try to avoid typing little L because you can easily confuse it with a big i. I <—> l (One can read your code as 1st or as the short form of List)

+1

Oh am sorry. i raised my query in higher window.will surely delete it.pls ignore.by the way thanks for the reply.i got my answer in your reply.

0

so if we replace append with sort(),add() the result would be the same ? Is that correct?

0

what method is returning explicit value in python?