append on lists

Chris Rebert clp at rebertia.com
Tue Sep 16 10:15:08 CEST 2008


On Tue, Sep 16, 2008 at 1:20 AM, Armin <a at nospam.org> wrote:
> John Machin wrote:
>>
>> On Sep 16, 6:45 am, Armin <a... at nospam.org> wrote:
>>
>>> Yes, but this is very unconvenient.
>>> If d should reference the list a extended with a single list element
>>> you need at least two lines
>>>
>>> a.append(7)
>>> d=a
>>>
>>> and not more intuitive d = a.append(7)
>>
>> Methods/functions which return a value other than the formal None and
>> also mutate their environment are "a snare and a delusion". Don't wish
>> for them.
>
>
>
>  c = [9,10]
>  [1,2,3,4,7].append(c) -> Is this a valid expression?

Literally, no, because you can't call methods on literals.
However, the sentiment is valid, though probably not what you want:

>>> c = [9,10]
>>> a = [1,2,3,4,7]
>>> b = a[:]
>>> a.append(c)
>>> a #note the nested list
[1, 2, 3, 4, 7, [9, 10]]
>>> b
[1, 2, 3, 4, 7]
>>> b.extend(c)
>>> b
[1, 2, 3, 4, 7, 9, 10]

Regards,
Chris

>
>  The 'value' of that expression is None.
>
>  However ... that's the way of the implementation of the append method.
>  It's a little bit confusing to me ...
>
> --Armin
>
> Thanks to all !
>
>
>>
>> Inconvenient? How often do you want to mutate a list and then set up
>> another reference to it?
>>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list