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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:30:33+00:00 2026-06-12T23:30:33+00:00

I have an object like this: t = {‘rand_key_1’: [‘x’], ‘rand_key_2’: [13,23], ‘rand_key_3’: [(1)],

  • 0

I have an object like this:

t = {'rand_key_1': ['x'], 'rand_key_2': [13,23], 'rand_key_3': [(1)], 'rk5': [1,100,3,4,3,3]}

a dictionary with random keys (string and/or int) which ALL have a list as a value, with varying sizes.

I want to turn this dictionary into an OrderedDict which is ordered depending on the Length of the list of the dictionary items. So after ordering I want to get:

t_ordered = {'rk5': ..., 'rand_key_2': .., 'rand_key_1': .., 'rand_key_3': ..}

(if two or more items have same value, their order do not really matter.

I tried this but I am failing:

OrderedDict(sorted(d, key=lambda t: len(t[1])))

I am not experiences so excuse me if what I try is uber stupid.

What can I do?

Thank you.

  • 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-12T23:30:35+00:00Added an answer on June 12, 2026 at 11:30 pm

    You were actually very close with the sorting function you passed to sorted. The thing to note is that sorted will return an interable of the dictionaries keys in order. So if we fix your function to index the dictionary with each key:

    >>> sorted(t, key=lambda k: len(t[k]))
    ['rand_key_3', 'rand_key_1', 'rand_key_2', 'rk5']
    

    You can also specify that the keys are returned in reverse order and iterating directly over these keys:

    >>> for sorted_key in sorted(t, key=lambda k: len(t[k]), reverse=True):
    ...     print sorted_key, t[sorted_key]
    
    rk5 [1, 100, 3, 4, 3, 3]
    rand_key_2 [13, 23]
    rand_key_3 [1]
    rand_key_1 ['x']
    

    Usually you wouldn’t need to create an OrderedDict, as you would just iterate over a new sorted list using the latest dictionary data.

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

Sidebar

Related Questions

I have an object like this, public class UserObj { public string First {get;
I have HasMap object like this: HashMap<String,String> fileCounter = new HashMap<String,String>(); fileCounter.put(Total Files,15); fileCounter.put(Total
If I have an object like this: struct Bar { std::string const& property(); };
I have a object like this: public class Test { public String TestValue {
I have an object like this: class MyObject { public string Object.Prop1 { get;
I have an object like this public class Employee { public String getName() {
Say I have an object-like data record like this: $article = array( 'title' =>
I'm just wondering .. I have and object like this public class Entry{ public
if I have an XML object like this: <a> <u date=2009-04-10 value=543/> <u date=2009-04-11
Suppose I have a javascript object like this: window.config config.UI = { opacity: {

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.