i = True
j = False
t = (i,j)
for i in t:
if i:
i = not i
print (t)
(True, False)
For example, I want to change all values in a tuple to False, no matter the element was True or False before. How can I realise that?
CodePudding user response:
You can do this with a list comprehension:
t = [False for _ in t]
or
t = tuple(False for _ in t)
if you want a tuple.
Your method doesn’t work because (imprecisely) i = not i
does not change the value of i
, it makes a new i
, with a different value, so the ‘original’ value in your tuple is unaffected.
CodePudding user response:
You need to create a new Tuple.
In fact it seems you need a Tuple with only False in it:
i = True
j = False
t = (i,j)
t = Tuple([False] * len(t))
CodePudding user response:
i = True
j = False
t = (i,j)
result = ()
for i in range(len(t)):
result = result (False,)
print(result)
CodePudding user response:
Inside the for
loop, do i = False
instead of i = not i
.