0

Say I have a list called list, which is comprised of boolean values. Also say that I have some (valid) index i which is the index of list where I want to switch the value.

Currently, I have: list[i] = not list[i].

But my question is, doesn't this iterate through list twice? If so is there are way to setup a temp value through aliasing to only iterate through the list once?

I tried the following:

temp = list[i]
temp = not temp

But this has not worked for me, it has only switched the value of temp, and not the value of list[i].

user1519226
  • 77
  • 2
  • 12

1 Answers1

0

you can look a little ways 'under the hood' using the dis module https://docs.python.org/3/library/dis.html

import dis


boolst = [True, True, True, True, True]

dis.dis('boolst[2] = not boolst[2]')
  1           0 LOAD_NAME                0 (boolst)
              2 LOAD_CONST               0 (2)
              4 BINARY_SUBSCR
              6 UNARY_NOT
              8 LOAD_NAME                0 (boolst)
             10 LOAD_CONST               0 (2)
             12 STORE_SUBSCR
             14 LOAD_CONST               1 (None)
             16 RETURN_VALUE

dis.dis('boolst[2] ^= True')
  1           0 LOAD_NAME                0 (boolst)
              2 LOAD_CONST               0 (2)
              4 DUP_TOP_TWO
              6 BINARY_SUBSCR
              8 LOAD_CONST               1 (True)
             10 INPLACE_XOR
             12 ROT_THREE
             14 STORE_SUBSCR
             16 LOAD_CONST               2 (None)
             18 RETURN_VALUE
f5r5e5d
  • 3,656
  • 3
  • 14
  • 18