append on lists

Steve Holden steve at holdenweb.com
Thu Sep 18 13:19:49 EDT 2008


Armin wrote:
> Duncan Booth wrote:
>> "Chris Rebert" <clp at rebertia.com> wrote:
>>> On Tue, Sep 16, 2008 at 1:20 AM, Armin <a at nospam.org> wrote:
>>>>  [1,2,3,4,7].append(c) -> Is this a valid expression?
>>> Literally, no, because you can't call methods on literals.
>>
>> Rubbish. There is no restriction about calling methods on literals.
>> That expression is perfectly valid but has no practical use that I can
>> see.
> 
> The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
> (with c = [8,9]) is identical, but the first expression doesn't provide
> a value. Strange by design ...
> 
Have a care, there. The semantics are different.

lst = [1, 2, 3, 4, 7]
lst.append([8, 9])

makes lst

[1, 2, 3, 4, 7, [8, 9]]

whereas

lst = [1, 2, 3, 4, 7]
lst = lst + [8, 9]

makes lst

[1, 2, 3, 4, 5, 7, 8, 9]

I suspect you meant [1, 2, 3, 4, 5, 7] + [c]

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list