Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 750307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:32:09+00:00 2026-05-14T14:32:09+00:00

I’m still learning python. I just wrote this method to determine if a player

  • 0

I’m still learning python. I just wrote this method to determine if a player has won a game of tic-tac-toe yet, given a board state like: '[['o','x','x'],['x','o','-'],['x','o','o']]'

def hasWon(board):
  players = ['x', 'o']
  for player in players:
    for row in board:
      if row.count(player) == 3:
        return player
    top, mid, low = board
    for i in range(3):
      if [ top[i],mid[i],low[i] ].count(player) == 3:
        return player
    if [top[0],mid[1],low[2]].count(player) == 3:
        return player
    if [top[2],mid[1],low[0]].count(player) == 3:
        return player
  return None

It occurred to me that I check lists of 3 chars several times and could refactor the checking to its own method like so:

def check(list, player):
  if list.count(player) == 3:
    return player

…but then realized that all that really does is change lines like:

 if [ top[i],mid[i],low[i] ].count(player) == 3:
    return player

to:

  if check( [top[i],mid[i],low[i]], player ):
    return player

…which frankly doesn’t seem like much of an improvement. Do you see a better way to refactor this? Or in general a more Pythonic option? I’d love to hear it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:32:10+00:00Added an answer on May 14, 2026 at 2:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you using UTF-8 perhaps, with a byte order mark… May 14, 2026 at 7:53 pm
  • Editorial Team
    Editorial Team added an answer I would guess that the Scanner is parsing bis which… May 14, 2026 at 7:53 pm
  • Editorial Team
    Editorial Team added an answer I've used geocoder.us for a few projects. They do require… May 14, 2026 at 7:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.