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

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 935
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.com>
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
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...
4
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,...
40
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...
11
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...
23
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...
7
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,...
2
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...
8
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...
0
by: Sean DiZazzo | last post by:
On Sep 20, 2:20 pm, Andrew <aet...@gmail.comwrote: What else would you expect to happen?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.