473,946 Members | 12,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1321
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
2512
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, so it becomes: handle.onreadystatechange = function() { whatever_was_previously_defined(whatever);some_other_function(handle) };
0
1090
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 form. This works fine but when I try to add new data to the XML file it does not comply with the XML schema file I am using.
2
1776
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"/> </sequence>
0
3011
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 implement those buttons, but I'm not sure what the code should look like. I tried what I thought was obvious code: myCollection.AddNew() and
2
15585
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 fine, except for one detail which I don't know how to handle. I have one element, which sometimes ends with a period, and sometimes not (the value of that element that is). In the output however, I need a period always. If I place a period inside...
14
18796
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 is that as more and more are added, population of the list box takes longer and longer. ie the first 10th of the item set are added much much quicker than the last 10th. THis occurs with about 40,000 listbox items. My worry is the listbox may...
14
3037
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 to implement. I had a special Malloc function that would allocated the number of needed bytes for the objects i needed to store + 1 additional byte to save the NULL character /*!
5
15607
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 from the elements that have this character set at their end,
0
1734
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 look for \\ and \n or just
0
10162
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9981
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11566
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11340
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10688
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9886
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6331
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4943
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3541
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.