Ranking players in a league by sorting the contents of an OrderedDict I'm new to python.  I'm having second thoughts on my approach to this problem:

The LeagueTable class tracks the score of each player in a league. After each game, the player records their score with the record_result function.
The player's rank in the league is calculated using the following
logic:

The player with the highest score is ranked first (rank 1). The player with the lowest score is ranked last.
If two players are tied on score, then the player who has played the fewest games is ranked higher.
If two players are tied on score and number of games played, then the player who was first in the list of players is ranked higher.

Implement the player_rank function that returns the player at the
given rank.
example:
table = LeagueTable(['Mike', 'Chris', 'Arnold'])
table.record_result('Mike', 2) 
table.record_result('Mike', 3)
table.record_result('Arnold', 5) 
table.record_result('Chris', 5)
print(table.player_rank(1))

All players have the same score. However, Arnold and Chris have played fewer games than Mike, and as Chris is before Arnold in the list of players, he is ranked first. Therefore, the code above should display &quot;Chris&quot;.


I have a solution, but I want to know if there is a more optimised approach to this.
from collections import Counter
from collections import OrderedDict

class LeagueTable:    
    def __init__(self, players):
        self.standings = OrderedDict([(player, Counter()) for player in players])
    def record_result(self, player, score):
        self.standings[player]['games_played'] += 1
        self.standings[player]['score'] += int(score)# print(self.standings)# print(&quot;&quot;)
    def player_rank(self, rank): #res = OrderedDict()
        x=self.standings
        d=self.standings
        def swap_list(xl,co):
            xl[co],xl[co+3]=xl[co+3],xl[co]
            xl[co+1],xl[co+4]=xl[co+4],xl[co+1]
            xl[co+2],xl[co+5]=xl[co+5],xl[co+2]
            return(xl,co)
        
        player_list=[]
        lc=len(x)
        lc1=lc
        lc=lc*3
        for p,c in x.items():
            player_list.append(p)
            player_list.append(c[&quot;score&quot;])
            player_list.append(c[&quot;games_played&quot;])
        for j in range(lc1):
            for i in range(0,lc-3,3):
                # print(i+4)
                if (player_list[i+1] &lt; player_list[i+4]):
                    swap_list(player_list,i)
                elif (player_list[i+1]==player_list[i+4]):
                    if(player_list[i+2]&gt;player_list[i+5]):
                        swap_list(player_list,i)
                    elif(player_list[i+2]==player_list[i+5]):
                        pass

        return(player_list[(rank-1)*3])
        
table = LeagueTable(['Mike', 'Chris', 'Arnold'])
table.record_result('Mike', 2)
table.record_result('Mike', 3)
table.record_result('Arnold', 5)
table.record_result('Chris', 5)
rk=int(input(&quot;enter rank \n&quot;))
print(table.player_rank(rk))
First of all, your entire def player_rank: is way over-engineered.. and with the correct standard modules this practically becomes a one-liner

Review


Use better variable names (naming is hard), but to me names like c, x makes no sense. I don't know what they do at all.
Use built in methods, like sort() this greatly reduces complexity.
With the full powers of sorted() you can sort multiple items at once.
With a - before the item we are sorting you can reverse the order, that makes it easier to sort multiple items on different orders. 


With my changes I get the following code.

from collections import OrderedDict, Counter

class LeagueTable:
    def __init__(self, players):
        self.standings = OrderedDict([(player, Counter()) for player in players])

    def record_result(self, player, score):
        self.standings[player]['games_played'] += 1
        self.standings[player]['score'] += score

    def player_rank(self, rank):
        return sorted(self.standings, key=lambda p: (-self.standings[p]['score'], 
                                                     self.standings[p]['games_played']))[rank-1]