0

I'm coding a Poker project and have just written a function to rescribe dealer, small and big blinds based on winner (pre-game). The code works, but it doesn't look pythonic:

players = {1:'Jose',2:'Sam',3:'John',4:'Victor'}
## Each player receives a card, John gets an Ace and wins the dealer spot
winner = 'John'

for num in players:
    if players[num] == winner:
        dealer = num
        
        if len(players) == 2:
            if num+1 <= len(players):
                small_blind = num+1
            else:
                small_blind = 1
                
        elif len(players) >= 3:
            if num+1 <=len(players):
                small_blind = num+1
                if num+2 <= len(players):
                    big_blind = num+2
                else:
                    big_blind = 1
            else:
                small_blind = 1
                big_blind = 2

print(f'{players[dealer]} will be dealing cards')
print(f'{players[small_blind]} will be small blind')
print(f'{players[big_blind]} will be big blind')

What is the most efficient way to loop through the entire list starting at a specified index?

pppery
  • 3,731
  • 22
  • 33
  • 46
VovaNapas
  • 3
  • 2
  • If there is no problem, then it looks off-topic here. You can check the rules on [CodeReview](https://codereview.stackexchange.com/) and post there according to their guidelines. Questions about best practice and efficiency are on topic there. – trincot Feb 19 '22 at 14:27
  • 1
    It sounds like you want to rotate a list. This [question](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python) may help. – quamrana Feb 19 '22 at 14:32
  • Yes, list rotation is exactly what I was looking for! – VovaNapas Feb 20 '22 at 16:30
  • There is also `itertools.cycle`, that provides [circular lists](https://stackoverflow.com/questions/23416381/circular-list-iterator-in-python). However solutions to your question using circular lists are less straightforward than the direct indexing in my answer. – Demi-Lune Feb 21 '22 at 13:25

1 Answers1

0

You could go directly with indices (mod length of list):

players = ['Jose', 'Sam', 'John', 'Victor']
winner = 'John'

dealer = players.index(winner)
small_blind = (dealer+1)%len(players)
big_blind   = (dealer+2)%len(players)
quamrana
  • 37,849
  • 12
  • 53
  • 71
Demi-Lune
  • 1,868
  • 2
  • 15
  • 26