[Tutor] About using list in a function

Steven D'Aprano steve at pearwood.info
Wed Aug 19 19:05:53 CEST 2015

Hi Michaelle, and welcome.

On Wed, Aug 19, 2015 at 12:09:15PM -0400, Michelle Meiduo Wu wrote:
> Hi there, I'm trying to use List in a function. But it doesn't work. 
> Here are sample code not work: 
> ---------------------------------------

> def getResult(): 
>     ls = [] 
>     ls = ls.append(100)

That line above is your problem. The append() method should be thought 
of as a procedure that acts in place, not a function which returns a 
value. So the line:

    ls = ls.append(100)

sets ls to None, a special value that means "no result". Instead, you 
should write this:

def getResult():
    ls = []
    return ls

Or you can make that even shorter:

def getResult():
    ls = [100, 200]
    return ls


More information about the Tutor mailing list