'how I can make my code much faster in python [closed]

Today I tried to answer one question with python but when I give it big inputs, output will come after 30 seconds.

Question :

in first line we give n. n equals to numbers we are going to give in second line. in second line we give you n numbers. in output you should print how many numbers are bigger than the next numbers ...

my code :

n = int(input())
players = list(input().split())
c = 0
j = 0

for i in players:
    players[j] = int(i)
    j += 1

while n:
    k = 0
    while k != n:
        if players[0] > players[k]:
            c += 1

        k += 1

    players.pop(0)
    n -= 1

print(c)

how I can make my code much faster (I tried using for instead of whiles but it was slower)



Solution 1:[1]

Try this one:

players = list(map(int, input().split()))
c = 0
for index, player in enumerate(players, 1):
    c += len([num for num in players[index:] if num < player])

I have tested it on 10k random integers as input. It is 3.25 sec against 13.08 sec of your code. Output is the same.

UPD:

little bit faster implementation

players = list(map(int, input().split()))
c1 = sum(len([num for num in players[index:] if num < player]) for index, player in enumerate(players, 1))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pL3b