user1291271
user1291271

Reputation: 71

boolean list reset

Is there a way to reset the below list back to all 'False' I have a program that uses a boolean list e.g.

bat=[False,False,False,False,False,False,False,False,False,False,False]

When the main loop does it's second iteration for the second team it still carries the 'True' values from the first team. I need to clear it up so the second team can record correct scores.

Any help will be much appreciated. Cheers

Upvotes: 0

Views: 253

Answers (2)

mVChr
mVChr

Reputation: 50185

A simple list comprehension should do the trick:

bat = [False for _ in bat]

Or multiply by len:

bat = [False] * len(bat)

EDIT: You should use bat[:] = for these assignments (see comments on larsmans' answer for why)

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363577

Why not just rebind the variable before each iteration? I.e.,

for team in teams:
    bat = [False] * 11
    # do whatever

If you must reset the list in-place, that's

bat[:] = [False] * 11

or

bat[:] = [False] * len(bat)

Upvotes: 4

Related Questions