[Tutor] Acting on objects in a list - how to make the change permanent?

Adam Cripps kabads at gmail.com
Mon Sep 27 21:59:01 CEST 2004


On 27 Sep 2004 11:19:57 -0400, Lloyd Kvam <pythontutor at venix.com> wrote:
> I did NOT read your source code, so this is simply a guess.   Assigning
> a modified list to a name does NOT change the original list.  Here's a
> simple example:
> 
> >>> def odd_only(numlist):
> ...     odds = [n for n in numlist if n%2]
> ...     numlist = odds
> ...     return odds
> ...
> >>> numlist = range(10)
> >>> odd_only(numlist)
> [1, 3, 5, 7, 9]
> >>> numlist
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> 
> The original numlist is NOT modified by the assignment in odd_only.
> 
> Since you are passing lists between functions, I am guessing that
> somewhere you have logic like the example above.  The simple fix here is
> to do:
>         numlist = odd_only(numlist)
> 
> Hope this helps.
> 
> 
> 
> Lloyd Kvam
> Venix Corp
> 
> 

Thanks Lloyd -

What would the syntax be for deleting an object from a list?
I've tried (with an error)
returned_list = del returned_list [intchoice]

Is there some other syntax that I should be trying here?

Thanks again.



-- 
--
proj: http://jiletters.sf.net
site: http://www.monkeez.org
wiki: http://wiki.monkeez.org


More information about the Tutor mailing list