Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7038363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:39:36+00:00 2026-05-28T01:39:36+00:00

Basically I have a nested list that I am trying to sort through the

  • 0

Basically I have a nested list that I am trying to sort through the 1’st index
I copied the way that the python howto says how to do it but it doesn’t seem to work and I don’t understand why:

code from the website:

>>> student_tuples = [
    ('john', 'A', 15),
    ('jane', 'B', 12),
    ('dave', 'B', 10),
    ]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
    [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

My code:

def print_scores(self):
    try:
        #opening txt and reading data then breaking data into list separated by "-"
        f = open(appdata + "scores.txt", "r")
        fo = f.read()
        f.close()
        userlist = fo.split('\n')
        sheet_list = []
        for user in userlist:
            sheet = user.split('-')
            if len(sheet) != 2:
                continue
            sheet_list.append(sheet)
        sorted(sheet_list, key = lambda ele : ele[1]) #HERE IS THE COPIED PART!
        if len(sheet_list) > 20: # only top 20 scores are printed
            sheet_list = sheet_list[len(sheet_list) - 21 :len(sheet_list) - 1]
       #prints scores in a nice table
        print "name          score"
        for user in sheet_list:
            try:
                name = user[0]
                score = user[1]
                size = len(name)
                for x in range(0,14):
                    if x > size - 1:
                        sys.stdout.write(" ")
                    else:
                        sys.stdout.write(name[x])
                sys.stdout.write(score + "\n")
            except:
                print ""


    except:
         print "no scores to be displayed!"

The bug is that the resulting printed list is exactly like how it was in the txt as if the sorting function didn’t do anything!

Example:

Data in txt file:

Jerry-1284
Tom-264
Barry-205
omgwtfbbqhaxomgsss-209
Giraffe-1227

What’s printed:

Name          Score
Jerry         1284
Tom           264
Barry         205
omgstfbbqhaxom209
Giraffe       1227
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:39:37+00:00Added an answer on May 28, 2026 at 1:39 am

    sorted returns a new list. If you want to modify the existing list, use

    sheet_list.sort(key = lambda ele : ele[1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I am trying to do is create ideally a nested List basically a
I have a tricky CSS situation here. Basically I have nested span tags that
I basically have an object, extended with a function through its prototype. Inside that
I basically have the following string in the format: A,B,C:D,E,F What I am trying
I basically have 7 select statements that I need to have the results output
Basically, I have a main container div with nested content. This div is has
I have a working application using Three20. It basically is a bunch of nested
I'm not sure if the title is very clear, but basically what I have
I have an ASP.NET page that uses a repeater nested within another repeater to
I have a nested structure that can't be edited because of some hosted jQuery,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.