I have attached two scripts which calculate the most popular songs according to zipf's law from the following standard input:6 3100 one50 two10 three30 four5 five10 sixThe first line details the number of songs to be input followed by the number of songs to return. Each line that follows holds the play counts and respective song titles. In the case of a tie, the algorithm must give precedence to the song that appeared earlier on the album.Both of my attempts to solve are currently not efficient enough. Any help in optimizing one or both of these would be greatly appreciated.Attempt 1:import sysfrom operator import itemgetterdef main():    line1 = sys.stdin.readline().split()    x = int(line1[0])-1    z = int(line1[1])    data =     for line in sys.stdin:        data+=line    data = data.split()    while(x >= 0):        data[2*x]= float(data[2*x]) * (x+1)        x-=1    answers = [(data[a+1], data[a]) for a in range(0,len(data),2)]    answers.sort(key=itemgetter(1), reverse=True)    p=0    while(p<z):        print answers[p][0]        p+=1main()Attempt 2:import sysfrom operator import itemgetterdef main():    line1 = sys.stdin.readline().split()    x = int(line1[0])-1    p = x    z = int(line1[1])    data =     for line in sys.stdin:        data+=line    data = data.split()    while(x >= 0):        data[2*x]= float(data[2*x]) * (x+1)        x-=1    y=0    list_of_lists = []    while(y<=p):        list_of_lists.append([data[2*y+1], data[2*y]])        y+=1    list_of_lists = sorted(list_of_lists, key=itemgetter(1), reverse=True)    n=0    while(n<z):        print list_of_lists[n][0]        n+=1main()Currently Cprofile reports are indicating that the first script is faster. Thanks!
Optimizing/Cleaning up Python Script for Zipf's Law
python;algorithm;performance