Replacing indexes with unknown list size

Viewed 39

How can I fill these index positions with an unknown size list?

For example if the list has 7 elements, how can I fill boardblanklis[winposition[X]] where X equals 0, 1, 2, 3, 4, 5, 6

I have included an example but it is for a list of 5 elements. I need it for a list of unknown number of elements, defined by user input. Probably need a loop.

for winposition in win:
    if boardblanklist[winposition[0]] == boardblanklist[winposition[1]] == boardblanklist[winposition[2]] == boardblanklist[winposition[3]] == boardblanklist[winposition[4]] == player:
        print('Congratulations! Player wins.')
        sys.exit()
1 Answers

Conditional

if all(boardblanklist[x] == player for x in winposition):

Full Usage

for winposition in win:
    if all(boardblanklist[x] == player for x in winposition):
        print('Congratulations! Player wins.')
        sys.exit()

Equivalent Code

for winposition in win:
    test = True
    for x in winposition: # All elements in winposition
        if boardblanklist[x] != player:
            test = False
            break
    if test:
       print('Congratulations! Player wins.')
       sys.exit()
Related