473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2352
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.