Check my understanding.

Professional audio recording and studio engineering, post #45,204
Author:
Date:
Subject:
 Tobiah
 2008-07-01 12:30:22
 Check my understanding.
list.append([1,2]) will add the two element list as the next
element of the list.

list.extend([1,2]) is equivalent to list = list + [1, 2]
and the result is that each element of the added list
becomes it's own new element in the original list.

Is that the only difference?

>From the manual:

s.extend(x) | same as s[len(s):len(s)] = x

But: (python 2.5.2)

>>> a
[1, 2, 3]
>>> a[len(a):len(a)] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable
>>>

Also, what is the difference between list[x:x] and list[x]?

>>> a[3:3] = [4]
>>> a
[1, 2, 3, 4]
>>> a[4] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>>


Thanks


** Posted from http://www.teranews.com **
Author:
Date:
Subject:
 Tobiah
 2008-07-01 12:33:55
 Re: Check my understanding.
Wrong group.... please ignore!

On Tue, 01 Jul 2008 12:30:22 -0700, Tobiah wrote:

> list.append([1,2]) will add the two element list as the next
> element of the list.
>
> list.extend([1,2]) is equivalent to list = list + [1, 2]
> and the result is that each element of the added list
> becomes it's own new element in the original list.
>
> Is that the only difference?
>
> From the manual:
>
> s.extend(x) | same as s[len(s):len(s)] = x
>
> But: (python 2.5.2)
>
>>>> a
> [1, 2, 3]
>>>> a[len(a):len(a)] = 4
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: can only assign an iterable
>>>>
>
> Also, what is the difference between list[x:x] and list[x]?
>
>>>> a[3:3] = [4]
>>>> a
> [1, 2, 3, 4]
>>>> a[4] = 5
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> IndexError: list assignment index out of range
>>>>
>
>
> Thanks
>
>
> ** Posted from http://www.teranews.com **

** Posted from http://www.teranews.com **
Author:
Date:
Subject:
 Lucky
 2008-07-01 17:48:22
 Re: Check my understanding.
Tobiah wrote:

> Wrong group.... please ignore!

And I was still working on it!
Author:
Date:
Subject:
 geoff
 2008-07-02 10:08:10
 Re: Check my understanding.
Lucky wrote:
> Tobiah wrote:
>
>> Wrong group.... please ignore!
>
> And I was still working on it!

I was going to say "Absolutely !".

geoff