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

adding a character to the last string element of a list

Hi,

I have the following question:

l = ['ABCDE','FGHI']
l[1:] #returns ['FGHI']
l[1:][0] #return 'FGHI'

a = l[1:][0] + 'J' #a becomes 'FGHIJ'

l[1:][0] += 'J' #NO ERROR BUT l[1:][0] == 'FGHI'
What am I missing ?

Thanks,

Philippe


Jul 21 '05 #1
2 1298
Philippe C. Martin wrote:
l = ['ABCDE','FGHI']
Okay so far...
l[1:] #returns ['FGHI']
Which is a _copy_ (via slicing) of part of the list. Another way of
saying this is that it is a _new_ list which has a copy of the
references from the appropriate part of the old list.

Try "l[1:] is l[1:]" to prove that...
l[1:][0] #return 'FGHI'
Sure does. From the new list.
a = l[1:][0] + 'J' #a becomes 'FGHIJ'
Because you are actually storing a reference to the new list, whose
first element you have modified.
l[1:][0] += 'J' #NO ERROR BUT l[1:][0] == 'FGHI'
You are modifying the first element of the *copy* of the slice of the
list, but you don't ever store a copy of it. When you try to check what
happened with the second part, you are creating yet another copy of part
of the list and sure enough the original has never been changed.
What am I missing ?


That slicing makes copies. If you directly access the element in the
first list (without using a slice) it will work.

(I think I've got most of the correct...)

-Peter
Jul 21 '05 #2
Thanks, I though it was a reference (tough to implement I'm sure)

Regards,

Philippe

Peter Hansen wrote:
Philippe C. Martin wrote:
l = ['ABCDE','FGHI']


Okay so far...
l[1:] #returns ['FGHI']


Which is a _copy_ (via slicing) of part of the list. Another way of
saying this is that it is a _new_ list which has a copy of the
references from the appropriate part of the old list.

Try "l[1:] is l[1:]" to prove that...
l[1:][0] #return 'FGHI'


Sure does. From the new list.
a = l[1:][0] + 'J' #a becomes 'FGHIJ'


Because you are actually storing a reference to the new list, whose
first element you have modified.
l[1:][0] += 'J' #NO ERROR BUT l[1:][0] == 'FGHI'


You are modifying the first element of the *copy* of the slice of the
list, but you don't ever store a copy of it. When you try to check what
happened with the second part, you are creating yet another copy of part
of the list and sure enough the original has never been changed.
What am I missing ?


That slicing makes copies. If you directly access the element in the
first list (without using a slice) it will work.

(I think I've got most of the correct...)

-Peter


Jul 21 '05 #3

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

Similar topics

5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
0
by: Michael Probst | last post by:
Hi all, I am new to .NET and the way XML data is handled in .NET I wrote a small application with .NET forms in C++ The application reads data from an XML file to fill-in the fields of the...
2
by: Faraz | last post by:
Hi, I am working with DOM and I need to do the following: <sequence> <element name="input1" type="string"/> <element name="input2" type="string"/> <element name="input3" type="string"/>...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
2
by: Sharkie | last post by:
I'm relatively new to XSLT, having strong background in bunch of other programming languages (Perl, Java, etc.). I'm writing an XSLT stylesheet, converting XML into HTML. Everything works just...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
14
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy...
5
by: dudeja.rajat | last post by:
Sorry : Earlier mail had a typo in Subject line which might look in-appropriate to my friends Hi, I've a list some of whose elements with character \. I want to delete this last character...
0
by: norseman | last post by:
dudeja.rajat@gmail.com wrote: ============================== depending on OS and other factors, the DirList may be more like: ABDIR\ 41 42 44 49 52 5C 0A nextDir If so endswith needs to...
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: 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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.