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

The Archive Base Latest Questions

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

I have a regular dictionary like: A = {37:4783, 92:47834, 12:234234,….} I need to

  • 0

I have a regular dictionary like:

A = {37:4783, 92:47834, 12:234234,....}

I need to return the second maximum value, the third, and so on. I was trying with:

max(A, key=lambda x: x[1])

but I got this error: TypeError: ‘float’ object is unsubscriptable

what am I doing wrong?

thanks

  • 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-12T15:59:32+00:00Added an answer on June 12, 2026 at 3:59 pm

    It doesn’t look like the keys are relevant at all. Therefore, you can just call sorted:

    >>> A = {37:4783, 92:47834, 12:234234}
    >>> sorted(A.values(), reverse=True)
    [234234, 47834, 4783]
    

    Your code

    max(A, key=lambda x: x[1])
    

    fails because iterating over a dictionary will yield its keys. Therefore, you are essentially calling

    max([37, 92, 12], key=lambda x:x[1])
    

    As you can see, the key doesn’t make any sense here; 37[1] will throw an error. If you want to sort the keys by the corresponding values, either sort the dictionary items or retrieve the value in the lambda function (or via dict.get):

    >>> [k for k,v in sorted(A.items(), key=lambda item: item[1], reverse=True)]
    [12, 37, 92]
    >>> sorted(A, key=lambda k: A[k], reverse=True)
    [12, 37, 92]
    >>> sorted(A, key=A.get, reverse=True)
    [12, 37, 92]
    

    Note that the latter may be slower since you need to retrieve every key from the dictionary (although dictionary access is really fast in Python).

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

Sidebar

Related Questions

I have passed to template regular Python dictionary and I need to inside $(document).ready(function()
I have a regular expression which allows the value range '1111' - '999999', excluding
I have a regular expression to validate a input value. At this point I
I have a regular texbox control. I need to highlight some words with a
I have an XML snippet like: <CharacterBlock MinVal=-10.000000 MaxVal=15.000000 Active=1> and I need to
I have read a cvs file and I would like to create a dictionary
I need help with building regular expression for text separating. Now I have some
I have a dictionary in .txt format, which looks like this: term 1 definition
i have a dictionary(?) of data returned from the simplejson.load() function. it looks like
I am trying to implement to search for a value in Python dictionary for

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.