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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:35:56+00:00 2026-05-18T07:35:56+00:00

I have a nested list: nested_list = [[‘a’, 3], [‘a’, 1], [‘a’, 5]] How

  • 0

I have a nested list:

nested_list = [['a', 3], ['a', 1], ['a', 5]]

How do I iterate over this list, select the sublist with the max integer value?

holder = []

for entry in nested_list:
    tmp = sublist with max entry[2] value
    holder.append(tmp)

I am stuck on coding the second line.

  • 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-18T07:35:57+00:00Added an answer on May 18, 2026 at 7:35 am

    try:

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

    or

    import operator
    
    max(nested_list, key=operator.itemgetter(1))
    

    If the first item will always be 'a', you can just do

    max(nested_list)
    

    If you’re willing to dive into some type checking and you want to do this for arbitrary sublists (Of one level only. something like [12, ‘a’, 12, 42, ‘b’]), you can do something like.

    import numbers
    
    max(nested_list, key=lambda x: max(i for i in x 
                                       if isinstance(i, numbers.Integral)))
    

    In any case, if you’re not sure that the elements of nested_list are in fact lists, you can do

    import collections
    
    max((s for s in nested_list 
         if isinstance(s, collections.Sequence)), 
        key=some_key_function)
    

    and just pass it a key function of your own devising or one of the other ones in this answer.

    In terms of the lambda x: x[1] vs. operator.itemgetter(1) question, I would profile. In princible, itemgetter should be the one right way but I’ve seen operator solutions get outperformed by lambda function do to ‘bugs’ (I use the term loosely, the code still works) in operator. My preference would be for itemgetter if performance doesn’t matter (and probably if it does) but some people like to avoid the extra import.

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

Sidebar

Related Questions

I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have a nested list something like this: PLACES = ( ('CA', 'Canada', (
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I have a nested list that looks like this: mylist = [['A;B', 'C'], ['D;E',
I have this deeply nested list (list of lists) and I want to replace
I have this nested list structure. My tree can go to n level. I
I have a nested list in my application : this.nestedList = new Ext.NestedList({ store:
I have a 3 level nested list in groovy like this rList = [[[12,
I have a nested list like below (but it has 1,000's of the holder

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.