Re: List problem
From: Peter Hansen (peter_at_engcorp.com)
Date: 10/31/04
- Next message: Peter Hansen: "Re: OT: Ruby programmers use Python (was Re: ruby on rails ... python on ?)"
- Previous message: Reinhold Birkenfeld: "Re: List problem"
- In reply to: Reinhold Birkenfeld: "Re: List problem"
- Next in thread: Reinhold Birkenfeld: "Re: List problem"
- Reply: Reinhold Birkenfeld: "Re: List problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 31 Oct 2004 08:43:28 -0500
Reinhold Birkenfeld wrote:
> Alex Martelli wrote:
>> a[:] = [x for x in a if x != 2]
>
> ^^^
> What are these for?
Observe the difference between these two approaches:
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]
>>> b = a = range(5)
>>> b is a
True
>>> a[:] = [x for x in a if x != 2]
>>> a
[0, 1, 3, 4]
>>> b
[0, 1, 3, 4]
>>> b is a
True
>>> b = a = range(5)
>>> a = [x for x in a if x != 2]
>>> b is a
False
[:] on the left side of the assignment basically causes
"slice assignment" to the whole list, modifying it in-place
instead of creating a new list.
-Peter
- Next message: Peter Hansen: "Re: OT: Ruby programmers use Python (was Re: ruby on rails ... python on ?)"
- Previous message: Reinhold Birkenfeld: "Re: List problem"
- In reply to: Reinhold Birkenfeld: "Re: List problem"
- Next in thread: Reinhold Birkenfeld: "Re: List problem"
- Reply: Reinhold Birkenfeld: "Re: List problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]