Why does list.append() return None?

I am trying to calculate a postfix expression using Python, but it did not work. I think this is maybe a Python-related problem.

Any suggestions?

expression = [12, 23, 3, '*', '+', 4, '-', 86, 2, '/', '+']

def add(a,b):
    return a + b
def multi(a,b):
    return a* b
def sub(a,b):
    return a - b
def div(a,b):
    return a/ b


def calc(opt,x,y):
    calculation  = {'+':lambda:add(x,y),
                     '*':lambda:multi(x,y),
                     '-':lambda:sub(x,y),
                     '/':lambda:div(x,y)}
    return calculation[opt]()



def eval_postfix(expression):
    a_list = []
    for one in expression:
        if type(one)==int:
            a_list.append(one)
        else:
            y=a_list.pop()
            x= a_list.pop()
            r = calc(one,x,y)
            a_list = a_list.append(r)
    return content

print eval_postfix(expression)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just replace a_list = a_list.append(r) with a_list.append(r).

Most functions, methods that change the items of sequence/mapping does return None: list.sort, list.append, dict.clear

Not directly related, but see Why doesn’t list.sort() return the sorted list?.

Method 2

The method append does not return anything:

>>> l=[]
>>> print l.append(2)
None

You must not write:

l = l.append(2)

But simply:

l.append(2)

In your example, replace:

a_list = a_list.append(r)

to

a_list.append(r)

Method 3

For return data on append use:

b = []   
a = b.__add__(['your_data_here'])

Method 4

append function mutates the list and it returns None. This is the piece of code which does that http://hg.python.org/cpython/file/aa3a7d5e0478/Objects/listobject.c#l791

listappend(PyListObject *self, PyObject *v)
{
    if (app1(self, v) == 0)
        Py_RETURN_NONE;
    return NULL;
}

So, when you say

a_list = a_list.append(r)

you are actually assigning a_list with None. So, the next time when you refer to a_list, it is not pointing to the list but the None. So, as others have suggested, change

a_list = a_list.append(r)

to

a_list.append(r)

Method 5

Functions like list.append(),list.sort() don’t return anything.
e.g

def list_append(p):
    p+=[4]

function list_append doesn’t have an return statement.so when you run following statements:

a=[1,2,3]
a=list_append(a)
print a
>>>None

but when you run following statements:

a=[1,2,3]
list_append(a)
print a
>>>[1,2,3,4]

That’s it.so,hoping it can help you.

Method 6

List methods can be divided in two types those who mutate the lists in place and return None (literally) and those who leave lists intact and return some value related to the list.

First category:

append
extend
insert
remove
sort
reverse

Second category:

count
index

The following example explains the differences.

lstb=list('Albert')
lstc=list('Einstein')

lstd=lstb+lstc
lstb.extend(lstc)
# Now lstd and lstb are same
print(lstd)
print(lstb)

lstd.insert(6,'|')
# These list-methods modify the lists in place. But the returned
# value is None if successful except for methods like count, pop.
print(lstd)
lstd.remove('|')
print(lstd)

# The following return the None value
lstf=lstd.insert(6,'|')
# Here lstf is not a list.
# Such assignment is incorrect in practice.
# Instead use lstd itself which is what you want.
print(lstf)

lstb.reverse()
print(lstb)

lstb.sort()
print(lstb)

c=lstb.count('n')
print(c)

i=lstb.index('r')
print(i)

pop method does both. It mutates the list as well as return a value.

popped_up=lstc.pop()
print(popped_up)
print(lstc)

Method 7

just a thought, instead of those functions (which manipulates the actual data) returning None, they should have returned nothing.
Then atleast the user would have caught the issue as it would have throwed an error stating some assignment error!!
Comment your thoughts!!

Method 8

Just in case somebody ends here, I encountered this behavior while trying to append on a return call

This works as expected

def fun():
  li = list(np.random.randint(0,101,4))
  li.append("string")
  return li

This returns None

def fun():
  li = list(np.random.randint(0,101,4))
  return li.append("string")


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x