473,672 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

explain slice assignment to newb

please explain this behavior to a newb:
>>a = [1,2,3,4]
b = ["a","b","c" ,"d"]
a
[1, 2, 3, 4]
>>b
['a', 'b', 'c', 'd']
>>a[0:2]
[1, 2]
>>a
[1, 2, 3, 4]
>>b[2:4]
['c', 'd']
>>a[0:2] = b[0:2]
b[2:4] = a[2:4]
a
['a', 'b', 3, 4]
>>b
['a', 'b', 3, 4]
>>>
Sep 20 '08 #1
2 958
Andrew wrote:
please explain this behavior to a newb:
Read the section on sequence slicing in the Library Reference. Use the
interactive interpreter or IDLE to perform experiments, like you did,
until you understand to your satisfaction.

Sep 21 '08 #2
On Sat, 20 Sep 2008 14:20:20 -0700 (PDT), Andrew <ae****@gmail.c om>
wrote:
>please explain this behavior to a newb:
>>>a = [1,2,3,4]
b = ["a","b","c" ,"d"]
>>>a[0:2] = b[0:2]
The slice [0:2] represent positions 0 <= x < 2
Replaces the [1, 2] from [1, 2, 3, 4] with ['a', 'b']
Result: a = ['a', 'b', 3, 4]
>>>b[2:4] = a[2:4]
The slice [2:4] represent positions 2 <= x < 4
Replaces the ['c', 'd'] from ['a', 'b', 'c', 'd'] with [3, 4]
Result: b = ['a', 'b', 3, 4]
>>>a
['a', 'b', 3, 4]
>>>b
['a', 'b', 3, 4]
Correct

Sep 21 '08 #3

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

Similar topics

0
1132
by: Rich Krauter | last post by:
I do not understand why python behaves the way it does in the following code: Here, Python doesn't like that I'm assigning out-of-range. OK. That's fine. >>> x = >>> x = 3 Traceback (most recent call last): File "<interactive input>", line 1, in ? IndexError: list assignment index out of range
4
2634
by: Jens Lippmann | last post by:
Hi all! I'm new to Python and just tried to assign values to a portion of a string, but I don't get it. My code is: bits = '\3ff' * bmi.bmiHeader.biSizeImage ofs = 0x1E2C0 for i in range(0, height): bits = self.shbits
40
2600
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end indexing) symmetrical and more consistent. So to find out if this is indeed a possibility, it would be nice to get a few opinions at this point. So blast away... or hopefully tell me what you like about it instead. ;-) (Any suggestions or...
11
4456
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
23
2325
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in tree.items('alfa.': 'beta.'): -- Antoon Pardon
7
2207
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69, Apr 11 2006, 15:32:42) on win32 Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last):
2
7246
by: smichr | last post by:
It seems to me that the indices() method for slices is could be improved. Right now it gives back concrete indices for a range of length n. That is, it does not return any None values. Using an example from clpy about this the indices for a 'None, None, -2' slice for a range of length 10 are given as '9, -1, -2'. The problem is that these concrete values cannot be fed back into a slice so that a slice will extract the same elements that...
8
1530
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering if someone could explain what's going on under the hood that makes index and slice assignments behave differently when assigning an empty list. For example:
0
764
by: Sean DiZazzo | last post by:
On Sep 20, 2:20 pm, Andrew <aet...@gmail.comwrote: What else would you expect to happen?
0
8488
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
8411
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
8932
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...
0
7449
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...
1
6240
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1821
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.