explain slice assignment to newb

T

Terry Reedy

Andrew said:
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.
 
S

Stephen Horne

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]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top