0

In python slice notation for lists and tuples, I noticed that a pipe character doesn't throw an error. I'm not sure what exactly it does though, as the results seem a little random.

testA = [1,2,3,4,5,6,7,8,9]

Given that

testA[0:3]

gives

[1, 2, 3]

and

testA[4:6]

gives

[5, 6]

What does this pipe do?

testA[0:3|4:6]

it gives

[1, 7]

Any ideas?

Coffee and Code
  • 885
  • 14
  • 16

1 Answers1

8
testA[0:3|4:6]

evaluates as

testA[0 : (3 | 4) : 6]

Which, in turn, evaluates via bitwise-or to

testA[0 : 7 : 6]

And this corresponds to the range 0 : 7 with a step size of 6. Hence only the first and last index are used.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214