473,388 Members | 1,213 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,388 software developers and data experts.

assignment statements: lists vs. strings

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
9 1634
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
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

"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
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
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
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
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
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
Thanks.
Jul 18 '05 #10

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
4
by: Pierre Barbier de Reuille | last post by:
Hello, a discussion began on python-dev about this. It began by a bug report, but is shifted and it now belongs to this discussion group. The problem I find with augmented assignment is it's...
50
by: John Salerno | last post by:
Is there a way to assign multiple variables to the same value, but so that an identity test still evaluates to False? e.g.: >>> w = x = y = z = False >>> w False >>> x False
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
35
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
3
by: Christian Christmann | last post by:
Hi, reading the output of gprof for one of my projects, I found that the STL list assignment operator consumes a larger fraction of the program's execution time. The exact entry in gprof's...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.