472,364 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

Please 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]
** Posted from http://www.teranews.com **
Jul 1 '08 #1
3 2309
On Jul 1, 12:35*pm, Tobiah <t...@tobiah.orgwrote:
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]
** Posted fromhttp://www.teranews.com**
In this example:
s.extend(x) | same as s[len(s):len(s)] = x
x _must_ be iterable. As the error states, `4` is not iterable.

the s[start:stop] notation is called slicing:
>>x = range(10)
x[0]
0
>>x[1]
1
>>x[0:1]
[0]
>>x[0:2]
[0, 1]
>>x[0:3]
[0, 1, 2]
>>x[1:3]
[1, 2]
>>x[5:-1]
[5, 6, 7, 8]
>>x[5:]
[5, 6, 7, 8, 9]
In general `x[len(x):len(x)] = seq` is a stupid way to extend a list,
just use .extend or +=.

Matt
Jul 1 '08 #2
On 1 juil, 21:35, Tobiah <t...@tobiah.orgwrote:
list.append([1,2]) will add the two element list as the next
element of the list.
list.append(obj) will add obj as the last element of list, whatever
type(obj) is.
list.extend([1,2]) is equivalent to list = list + [1, 2]
Not quite. The second statement rebinds the name list (a very bad name
BTW but anyway...) to a new list object composed of elements of the
list object previously bound to the name list and the elements of the
anonymous list object [1, 2], while the first expression modifies the
original list object in place. The results will compare equal (same
type, same content), but won't be identical (not the same object).

A better definition for list.extend(iterable) is that it is equivalent
to:

for item in iterable:
list.append(item)

The difference is important if list is bound to other names. A couple
examples:

a = [1, 2, 3}
b = a
# b and a points to the same list object
b is a
=True

a.append(4)
print b
=[1, 2, 3, 4]

b.extend([5, 6])
print a
=[1, 2, 3, 4, 5, 6]

a = a + [7, 8]
print b
=[1, 2, 3, 4, 5, 6]

print a
=[1, 2, 3, 4, 5, 6, 7, 8]

a is b
=False

def func1(lst):
lst.extend([9, 10])
print lst

def func2(lst):
lst = lst + [11, 12]
print lst

func1(a)
=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print a
=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

func2(a)
=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
print a
=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Is that the only difference?
cf above.
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
And if you try with extend, you'll also have a TypeError:

a.extend(4)
=Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
list.extend expects an iterable, and so does slice assignment.

You want:

a[len(a):len(a)] = [4]
>
Also, what is the difference between list[x:x] and list[x]?
The first expression refers to the *sublist* starting at x and ending
one element before x. Of course, if x == x, then it refers to an empty
list !-)
>>a[3:3]
[]
>>a[1:3]
[2, 3]
>>a[0:2]
[1, 2]
>>a[0:1]
[1]
>>>
The second expression refers to the *element* at index x.

HTH
Jul 1 '08 #3
On Tue, 01 Jul 2008 12:35:01 -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.
It's not 100% equivalent because `list.extend()` mutates the original list
while ``+`` creates a new list object:

In [8]: a = [1, 2, 3]

In [9]: b = a

In [10]: b.extend([4, 5])

In [11]: b
Out[11]: [1, 2, 3, 4, 5]

In [12]: a
Out[12]: [1, 2, 3, 4, 5]

In [13]: b = b + [6, 7]

In [14]: b
Out[14]: [1, 2, 3, 4, 5, 6, 7]

In [15]: a
Out[15]: [1, 2, 3, 4, 5]
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
>>>>
Have you tried `extend()` with the same value?

In [15]: a
Out[15]: [1, 2, 3, 4, 5]

In [16]: a.extend(6)
---------------------------------------------------------------------------
<type 'exceptions.TypeError' Traceback (most recent call last)

/home/bj/<ipython consolein <module>()

<type 'exceptions.TypeError'>: 'int' object is not iterable

See, both ways need something iterable.

Ciao,
Marc 'BlackJack' Rintsch
Jul 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Daz | last post by:
Hello hello! I'm trying to finish off putting my design into HTML and I've come across a problem that I can't get my head around. I've got divs floating in two columns, but I'm having problems...
4
by: John | last post by:
Hi all, I have posted this type of question quite a few times but to date, no-one has actually been able to provide me with a solution. I really need to understand how to do this properly. My...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
22
by: rasiel | last post by:
I'm hoping someone can help me out. I'm a researcher in need of developing an automated database and would like to see if someone here is willing to consider putting together for me a simple...
4
by: garyusenet | last post by:
The following code was supplied by a kind poster as a solution to a problem i was having. But it's not quite working. I have commented the code myself below. Can you please read my comments to make...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.