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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:17:04+00:00 2026-06-01T19:17:04+00:00

I have the following Python array of dictionaries: myarr = [ { ‘name’: ‘Richard’,

  • 0

I have the following Python array of dictionaries:

myarr = [ { 'name': 'Richard', 'rank': 1 },
{ 'name': 'Reuben', 'rank': 4 },
{ 'name': 'Reece', 'rank': 0 },
{ 'name': 'Rohan', 'rank': 3 },
{ 'name': 'Ralph', 'rank': 2 },
{ 'name': 'Raphael', 'rank': 0 },
{ 'name': 'Robin', 'rank': 0 } ]

I’d like to sort it by the rank values, ordering as follows: 1-2-3-4-0-0-0.

If I try:

sorted_master_list = sorted(myarr, key=itemgetter('rank'))

then the list is sorted in the order 0-0-0-1-2-3-4.

How can I define a custom comparator function to push zeroes to the bottom of the list? I’m wondering if I can use something like methodcaller.

  • 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-06-01T19:17:05+00:00Added an answer on June 1, 2026 at 7:17 pm

    Option 1:

    key=lambda d:(d['rank']==0, d['rank'])
    

    Option 2:

    key=lambda d:d['rank'] if d['rank']!=0 else float('inf')
    

    Demo:

    “I’d like to sort it by the rank values, ordering as follows: 1-2-3-4-0-0-0.” –original poster

    >>> sorted([0,0,0,1,2,3,4], key=lambda x:(x==0, x))
    [1, 2, 3, 4, 0, 0]
    
    >>> sorted([0,0,0,1,2,3,4], key=lambda x:x if x!=0 else float('inf'))
    [1, 2, 3, 4, 0, 0]
    

     

    Additional comments:

    “Please could you explain to me (a Python novice) what it’s doing? I can see that it’s a lambda, which I know is an anonymous function: what’s the bit in brackets?” – OP comment

    Indexing/slice notation:

    itemgetter('rank') is the same thing as lambda x: x['rank'] is the same thing as the function:

    def getRank(myDict):
        return myDict['rank']
    

    The [...] is called the indexing/slice notation, see Explain Python's slice notation – Also note that someArray[n] is common notation in many programming languages for indexing, but may not support slices of the form [start:end] or [start:end:step].

    key= vs cmp= vs rich comparison:

    As for what is going on, there are two common ways to specify how a sorting algorithm works: one is with a key function, and the other is with a cmp function (now deprecated in python, but a lot more versatile). While a cmp function allows you to arbitrarily specify how two elements should compare (input: a,b; output: a<b or a>b or a==b). Though legitimate, it gives us no major benefit (we’d have to duplicate code in an awkward manner), and a key function is more natural for your case. (See “object rich comparison” for how to implicitly define cmp= in an elegant but possibly-excessive way.)

    Implementing your key function:

    Unfortunately 0 is an element of the integers and thus has a natural ordering: 0 is normally < 1,2,3… Thus if we want to impose an extra rule, we need to sort the list at a “higher level”. We do this by making the key a tuple: tuples are sorted first by their 1st element, then by their 2nd element. True will always be ordered after False, so all the Trues will be ordered after the Falses; they will then sort as normal: (True,1)<(True,2)<(True,3)<..., (False,1)<(False,2)<..., (False,*)<(True,*). The alternative (option 2), merely assigns rank-0 dictionaries a value of infinity, since that is guaranteed to be above any possible rank.

    More general alternative – object rich comparison:

    The even more general solution would be to create a class representing records, then implement __lt__, __gt__, __eq__, __ne__, __gt__, __ge__, and all the other rich comparison operators, or alternatively just implement one of those and __eq__ and use the @functools.total_ordering decorator. This will cause objects of that class to use the custom logic whenever you use comparison operators (e.g. x=Record(name='Joe', rank=12) y=Record(...) x<y); since the sorted(...) function uses < and other comparison operators by default in a comparison sort, this will make the behavior automatic when sorting, and in other instances where you use < and other comparison operators. This may or may not be excessive depending on your use case.

    Cleaner alternative – don’t overload 0 with semantics:

    I should however point out that it’s a bit artificial to put 0s behind 1,2,3,4,etc. Whether this is justified depends on whether rank=0 really means rank=0; if rank=0 are really “lower” than rank=1 (which in turn are really “lower” than rank=2…). If this is truly the case, then your method is perfectly fine. If this is not the case, then you might consider omitting the 'rank':... entry as opposed to setting 'rank':0. Then you could sort by Lev Levitsky’s answer using 'rank' in d, or by:

    Option 1 with different scheme:

    key=lambda d: (not 'rank' in d, d['rank'])
    

    Option 2 with different scheme:

    key=lambda d: d.get('rank', float('inf'))
    

    sidenote: Relying on the existence of infinity in python is almost borderline a hack, making any of the mentioned solutions (tuples, object comparison), Lev’s filter-then-concatenate solution, and even maybe the slightly-more-complicated cmp solution (typed up by wilson), more generalizable to other languages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array in Python: points_list = [point0, point1, point2] where each
Let's say I have the following empty two dimensional array in Python: q =
I am new in python.I have data in a 2d- array in the following
I have the following code in Python using Numpy: p = np.diag(1.0 / np.array(x))
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
I have the following simple python test script that uses Suds to call a
I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have the following simple methods for writing a python object to a file

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.