[Tutor] Using sorted in Python 3.3.5

Greg Christian glchristian at comcast.net
Thu Nov 19 10:31:47 EST 2015


I’m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You.

def getKey(item):
    return item[1]

def counter():
    L = [("baby", 9999), ("aeuron", 100), ("pablo", 1234)]
    sorted(L, key=getKey)
    print ("L = ", L)

OUTPUTS THIS (when calling counter inside of program):

L =  [('baby', 9999), ('aeuron', 100), ('pablo', 1234)]

OUTPUTS THIS (when running with IDLE – desired output):

[('aeuron', 100), ('pablo', 1234), ('baby', 9999)]


More information about the Tutor mailing list