Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '[1,': 0.09; 'function,': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'exist.': 0.16; 'received:gmx.com': 0.16; 'subject: \n ': 0.16; 'subject:skip:u 10': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'exists': 0.24; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; '[1]': 0.29; 'subject:list': 0.30; "i'm": 0.30; 'from:addr:gmx.com': 0.31; 'could': 0.34; 'data,': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'anything': 0.39; 'does': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'email addr:gmail.com': 0.63; 'charset:windows-1252': 0.65; 'received:74.208': 0.68 Date: Wed, 14 Jan 2015 00:49:06 +1100 From: mortoxa User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Help understanding list operatoins inside functions in python 3 References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------090809090000090308050509" X-Provags-ID: V03:K0:mcRD1yXINtkXyDbLQHxRq2RJv2Jrf90tvIj8WXNDpAVjCUnk7YB ZdoIk7ARgIfEoTI+zP1TYC9fxN0RXDO3ERCiVbPsH6vVek30d2wEFADq6HW/WbQVEodP7p0 MNXZv0DTwiZdsRdupZGYkemFnbPj0NF9jDBw5J9aWdJZ5wtPllLmDecK3J5QYjY4UP7UmAs bvKRC95ERUXZNTOweSQWw== X-UI-Out-Filterresults: notjunk:1; X-Mailman-Approved-At: Tue, 13 Jan 2015 15:24:26 +0100 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 135 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421159068 news.xs4all.nl 2886 [2001:888:2000:d::a6]:45637 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83693 This is a multi-part message in MIME format. --------------090809090000090308050509 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 01/13/15 23:51, stephen.boulet@gmail.com wrote: > I'm a bit confused why in the second case x is not [1,2,3]: > > x = [] > > def y(): > x.append(1) > > def z(): > x = [1,2,3] > > y() > print(x) > z() > print(x) > > Output: > [1] > [1] In your y() function, as you are appending data, the list must already exist. So the global list x is used In your z() function, you are creating a local list x which only exists as long as you are in the function. Anything you do to that list has no effect on the global list x. That is why the list does not change. If you specifically wanted to change the global list x, you could do this: def z(): global x x = [1, 2, 3] Output: [1] [1, 2, 3] Or better def z(): x = [1, 2, 3] return x y() print(x) x = z() print(x) Output: [1] [1, 2, 3] --------------090809090000090308050509 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit
On 01/13/15 23:51, stephen.boulet@gmail.com wrote:
I'm a bit confused why in the second case x is not [1,2,3]:

x = []

def y():
    x.append(1)

def z():
    x = [1,2,3]

y()
print(x)
z()
print(x)

Output:
[1]
[1]

In your y() function, as you are appending data, the list must already exist. So the global list x is used

In your z() function, you are creating a local list x which only exists as long as you are in the function.
Anything you do to that list has no effect on the global list x. That is why the list does not change.

If you specifically wanted to change the global list x, you could do this:

def z():
    global x
    x = [1, 2, 3]

Output:
[1]
[1, 2, 3]

Or better

def z():
    x = [1, 2, 3]
    return x

y()
print(x)
x = z()
print(x)

Output:
[1]
[1, 2, 3]
--------------090809090000090308050509--