I might use
def check(somelist, player):
return somelist.count(player) == 3
Edit: as @Andrew suggested in a comment (tx @Andrew!), you can do even better, e.g.:
def check(somelist, player):
return somelist.count(player) == len(somelist)
without hardcoding the 3
-- which also suggests another nice alternative:
def check(somelist, player):
return all(x==player for x in somelist)
which very directly reads "all items in the list equal player
". The general point is that by refactoring to a separate method you can then play with that method s implementation -- now of course here the code is very simple so the advantage is similarly modest, but it s an excellent point to keep in mind as you move to more complicated code.
As you ve noticed you only need a bool anyway, so this allows a much simpler approach -- just return the bool expression rather than doing an if
on it. It s important to never use a built-in name like list
for your own identifiers -- an "attractive nuisance" of the language...;-).
By which I mean, Python uses for its built-ins lots of nice, attractive names like list, bool, sum, and so on, so it s easy to find yourself accidentally using one of those names for a variable of your own, and nothing bad seems to happen... until the time you need to turn, say, a tuple into a list, use the obviously best solution, x = list(thetuple)
... and end up spending our trying to understand and solve the errors that come because you ve used list
to mean anything else than the built-in type of that name.
So, just get into the habit of not using those nice built-in names for purposes other than indicating the respective builtins, and you ll save yourself much future aggravation!-)
Back to your code, you might consider the conciseness afforded by not unpacking board
(a hard decision, since your code is quite readable... but may look a bit verbose):
for i in range(3):
if check([row[i] for row in board], player):
return player
if check([row[i] for i, row in enumerate(board)], player):
return player
if check([row[2-i] for i, row in enumerate(board)], player):
return player
In the end I think I d stick with your choice -- more readable and just marginally more verbose, if at all -- but it s nice to be aware of the alternatives, I think -- here, list comprehensions and enumerate
to generate the lists to be checked as an alternative to "manually coding out" the three possibilities.