By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

assignment statements: lists vs. strings

P: n/a
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla' string2 'bla' list1 = [1,2]
list2 = list1
list1.append(1)
list1 [1, 2, 1] list2 [1, 2, 1]

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
kl************@yahoo.de (Klaus Neuner) wrote in
news:3e**************************@posting.google.c om:
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla' string2 'bla' list1 = [1,2]
list2 = list1
list1.append(1)
list1 [1, 2, 1] list2 [1, 2, 1]


You aren't comparing like with like here. You can do exactly the same as
your string example using a list:
list1 = [1,2]
list2 = list1
list1 = list1 + [3,4]
list1 [1, 2, 3, 4] list2 [1, 2]


The difference is that addition of strings, or lists, or anything else,
creates a new object. The append method of the list type modifies an
existing object. The string types don't have an append method because they
are IMMUTABLE: immutable objects, once created never change their values.
Mutable objects can change their values.

Remember that assignment always creates another reference to an existing
object, it never makes a copy of an object.

Builtin immutable objects include: int, long, float, str, unicode, tuple
Builtin mutable objects include: list, dict
Jul 18 '05 #2

P: n/a
On 2 Feb 2004 07:50:01 -0800, kl************@yahoo.de (Klaus Neuner)
wrote:
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1'blabla' string2'bla' list1 = [1,2]
list2 = list1
list1.append(1)
list1[1, 2, 1] list2[1, 2, 1] Strings are immutable, lists are mutable. String concatenation builds
a new string object. List appending does not build a new list.
Variables/names are references to objects; they are not themselves the
objects.

In the first example, you point both variables/names to the "bla"
string. You then build a new string object "blabla" and point the
string1 variable to that new string. string2 still points to the old
one.

In the second example, you point two variables/names to the [1, 2]
list. You then append an item to that list. The two variables still
point to the same list, which now has an additional element.

The "what is it good for" is the mutable/immutable difference. Think
of it this way--numbers are immutable. If you said: n1 = 3
n2 = n1
n1 = n1 + 1
print n1, n2

You wouldn't expect the number 3 itself to change. When you add n1 to
3, a new object with the value 4 is created and n1 is reassigned to
point to it. Just as with the string, n2 still points to the "old" 3
object.
--dang
p.s.
This last bit about the numbers is supposed to be conceptual, rather
than a description of the internal implementation.
Jul 18 '05 #3

P: n/a

"Klaus Neuner" <kl************@yahoo.de> wrote in message
news:3e**************************@posting.google.c om...
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings:


You are doing different operations, and hence should not expect the same
result.

string1 = "bla"
string2 = string1
string1 = string1 + "bla"
Here you add two strings together to get a third, which you bind back to
the first name
string1 'blabla' string2 'bla'

list1 = [1,2]
list2 = list1
list1.append(1)
Here you mutate a list in place, which you can do because lists are mutable
and which you cannot do with strings. To parallel your string operation,
try

list1 = list1 + [1,2]

before you mutate list1 and you should get [1,2,1,2]

list1 [1, 2, 1] list2

[1, 2, 1]


Terry J. Reedy


Jul 18 '05 #4

P: n/a
In article <3e**************************@posting.google.com >,
Klaus Neuner <kl************@yahoo.de> wrote:

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?


http://starship.python.net/crew/mwh/...jectthink.html
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
Jul 18 '05 #5

P: n/a
On 2 Feb 2004, Klaus Neuner <- kl************@yahoo.de wrote:
I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?


Strings are immutable and lists are mutable objects.
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
Here you create a new string string1 which is fresh allocated. string1 'blabla' string2 'bla'


If you did `id(string1)' and `id(string2)' before and after the
assignment you would have that after the assignment you had two
different objects.
list1 = [1,2]
list2 = list1
list1.append(1)
Here you modify an existing list in place. But that's not the same as
you did with your string exapmle. With a string you couldn't do that
since a string is immutable.

If you had written list1 = list1 + [1] you would have had a new list
object.
L1 = [1,2]
L2 = L1
id(L1) 10489068 id(L2) 10489068 L1 = L1 + [1]
id(L1) 10488140 id(L2) 10489068 L2 [1, 2] L1 [1, 2, 1]

KP

--
If you have nothing to say on a subject, replying with a line such as,
"I agree with this." puts you in the TO:'s for all future messages, and
establishes you as "one who really cares", if not an actual expert, on
the topic at hand. -- from the Symbolics Guidelines for Sending Mail
Jul 18 '05 #6

P: n/a
kl************@yahoo.de (Klaus Neuner) wrote in message news:<3e**************************@posting.google. com>...
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla' string2 'bla' list1 = [1,2]
list2 = list1
list1.append(1)
This is *not* equivalent to the string code. Notice that
list1 = list1 + [1]
list1 [1, 2, 1] list2 [1, 2]

*does* behave like your string example.
list1 [1, 2, 1] list2

[1, 2, 1]


Unlike string1, the name "list1" hasn't been rebound since the
assignment "list2 = list1", and therefore they still refer to the same
object.
Jul 18 '05 #7

P: n/a
Thank you all. I have understood the point now.

Klaus

I would like to ask (Duncan) another question, although it is off
topic: I have seen those small photographs in the header of articles
quite often. How are they produced?
Jul 18 '05 #8

P: n/a
kl************@yahoo.de (Klaus Neuner) wrote in
news:3e**************************@posting.google.c om:
Thank you all. I have understood the point now.

Klaus

I would like to ask (Duncan) another question, although it is off
topic: I have seen those small photographs in the header of articles
quite often. How are they produced?


Do a Google search for X-Face. Some newsreaders recognise the X-Face header
line as containing a small 48x48 pixel monochrome bitmap. You can download
programs to convert from more usual bitmap formats into X-Face format then
you just have to include the header line in all your posts; most
newsreaders will let you add custom header lines fairly easily.
Jul 18 '05 #9

P: n/a
Thanks.
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.