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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:19:28+00:00 2026-06-18T09:19:28+00:00

I am trying to sort a dictionary containing lists. For example, if I have

  • 0

I am trying to sort a dictionary containing lists. For example, if I have this dictionary:

a = {'q': {3: [4, 2, 7]}, 'a': {1: [5, 45, 11]}, 'e': {23: [11, 45, 2]}}

I want the output after sorting to be:

[(e, {23:[11,45,2}]), (a, {1:[5,45,11]}), (q,{3,[4,2,7]})] 

I am actually sorting in reverse, using the first item in the list as the key for the sort.

In the event that the first items of two lists are identical, like above, I sort for the string associated with the list (main key) in alphabetical order.

I am not sure if I can get the output of tuples with dictionary in it as I am sorting for that the list in that dictionary.

I have tried this code:

sorted((x,b.items(), key=lambda x:[1][2]) for x,b in a.items())

It raised an error for invalid syntax, and I can’t figure out what’s wrong.

  • 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-18T09:19:29+00:00Added an answer on June 18, 2026 at 9:19 am

    Let’s break the problem in parts. You really want to sort the list a.items(). So:

    >>> to_sort = a.items()
    >>> to_sort
    [('q', {3: [4, 2, 7]}), ('a', {1: [5, 3, 11]}), ('e', {23: [11, 45, 2]})]
    

    Now, for each element in the list, you have a tuple of a value ('q' etc.) and a dictionary. Presumably, each dictionary contains only one key, and you want to use the index-1 element of each dictionary’s value as the primary key. So, the key for the first element should be: to_sort[0][1].values()[0][1]: to_sort[0][1] gives you the dictionary {3: [4, 2, 7]}, .values() gives you the list [[4, 2, 7]], and [0][1] on that gives you 2. The secondary sort key is simply to_sort[0].

    So we get:

    >>> sorted(to_sort, key=lambda x: (x[1].values()[0][1], x[0]))
    [('q', {3: [4, 2, 7]}), ('a', {1: [5, 3, 11]}), ('e', {23: [11, 45, 2]})]
    

    We are almost there. Now you just need to tell sort that you want reversed output:

    >>> sorted(to_sort, key=lambda x: (x[1].values()[0][1], x[0]), reverse=True)
    [('e', {23: [11, 45, 2]}), ('a', {1: [5, 3, 11]}), ('q', {3: [4, 2, 7]})]
    

    Is this what you want?

    If you want a one-liner, you can do:

    >>> sorted(a.items(), key=lambda x: (x[1].values()[0][1], x[0]), reverse=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unsorted Dictionary<int, Dictionary<int, string>> and when trying to sort its value's
I have dictionary Dictionary<string, Point> the Key is c1,c3,c2,t1,,t4,t2 I want to sort them
I am trying to have some sort of Data Object (I'm thinking a dictionary)
I'm trying to sort a dictionary by value with LINQ but I can't figure
Trying to sort this array of objects according to (1) depth and (2) weight,
Been trying to sort this one out for a while. I'd really appreciate any
I'm trying to sort by the state of an Organization's main address. I have
I am trying to sort a two-dimensional array by a timestamp column Descending. This
I am trying to sort pixel values of an image (example 80x20) from lowest
I am using Python 2.6.2. I have a dictionary, graph which has tuple (source,

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.