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].

2
  • doesn't this iterate through list twice? Does it? Why do you think so? Commented Oct 24, 2017 at 2:05
  • It doesn't iterate the list at all? I'm not understanding what you mean. Commented Oct 24, 2017 at 2:21

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.