-2

For example:

x = [0] * 10
x[2:8] = [1] * 2

the result becomes:

[0,0,1,1,0,0]

Why is that happening? I didn't change the length of x but it automatically changed into 6.

Moreover, the assignment is assign to the middle part of the x list, could anybody tell me why?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Yang Liu
  • 45
  • 1
  • 5

1 Answers1

3

You did change the length. You selected the length-6 slice from 2 to 8 (not including 8) and replaced it with a list of length 2 ([1] * 2). In other words, you cut out the middle of the list and put a smaller middle back in its place.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384