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 1033225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:12:01+00:00 2026-05-16T14:12:01+00:00

Obs: I know lists in python are not order-fixed, but think that this one

  • 0

Obs: I know lists in python are not order-fixed, but think that this one will be.
And I’m using Python 2.4

I have a list, like (for example) this one:

mylist = [ ( u'Article', {"...some_data..."}    ) ,
           ( u'Report' , {"...some_data..."}    ) ,
           ( u'Book'   , {"...another_data..."} ) ,
...#continue
]

This variable mylist is obtained from a function, and the ‘order’ of the list returned will vary. So, sometimes it will be like on the example. Sometimes, the ‘Report’ will come before ‘Article’, etc.

I have a fixed order that I want on this list (and isn’t the alphabetical).

Let’s say that my fixed order is: ‘Report’, ‘Article’, ‘Book’, …

So, what I want is that: whatever order ‘mylist’ is instantiated, I want to reorder it making ‘Report’ stay on front, ‘Article’ on second, etc…

What’s the best approach to reorder my list (taking the first element of the tuple of each item on list) using my ‘custom’ order?

Answer:

I ended up with this:

mylist became a list of dicts, like this:

mylist = [{'id':'Article', "...some_data..."} ,
        ...etc
]

each dict having a ‘id’ that had to be sorted.

Saving the correct order on a listAssigning the correct_order on a list:

correct_order = ['Report', 'Article', 'Book', ...]

and doing:

results = sorted([item for item in results], cmp=lambda x,y:cmp(correct_order.index(x['id']), correct_order.index(y['id'])))
  • 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-16T14:12:02+00:00Added an answer on May 16, 2026 at 2:12 pm

    You could use a dictionary that would map every first element to its “weight” and then check this dictionary inside a sorting function.

    Something like:

    d = { "Report": 1,
          "Article": 2,
           "Book": 3 }
    result = sorted(mylist, key=lambda x:d[x[0]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand why. I know, this name is really not valid. But this table
I have some data in this mysql table, but it is not showing anything
I know its not the usual thing to do. But the specification I'm implementing
I have a dataframe in R that I loaded from a CSV file. One
I have a linq query with NHibernate using Session.Query<T> method and I in this
I have this situation: An object that it's observable and another object that it
(OBS: English is not my native language and I understand that the title of
I'm trying to link the items from a specific column, but each one will
I have a df with 100k+ obs and 12 cols. One of those cols
I have a timeseries of samples in R: > str(d) 'data.frame': 5 obs. of

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.