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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:27:23+00:00 2026-06-15T13:27:23+00:00

Suppose I have a nested list as the one below: [[[‘a’],[24],214,1] ,[[‘b’],[24],312,1] ,[[‘a’],[24],3124,1] ,

  • 0

Suppose I have a nested list as the one below:

[[['a'],[24],214,1] ,[['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1]]

and suppose I want to remove from the list all the items EXCEPT the one that has the maximum value for item[2] among items sharing the same letter in item[0]

So for example in the previous list I have two items sharing the same letter in item[0]:

[ ['a'],[24],214,1], [['a'],[24],3124,1] ]

and I want to remove the former since it has a lower value for item[2].

The output list should then be:

[ [['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1] ]

Can you suggest me a compact way to do it?

  • 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-15T13:27:24+00:00Added an answer on June 15, 2026 at 1:27 pm

    As your question is confusing, I have given the possibility of removing both the max and the min elements

    >>> def foo(some_list, fn = max):
        #Create a dictionary, default dict won;t help much as 
        #we have to refer back to the value for an existing key
        #The dictionary would have item[0] as key
        foo_dict = dict()
        #Iterate through the list
        for e in some_list:
                #Check if the key exist
            if e[0][0] in foo_dict:
                        #and if it does, find the max of the existing value and the 
                        #new element. The key here is the second item
                foo_dict[e[0][0]] = fn(foo_dict[e[0][0]], e, key = lambda e:e[2])
            else:
                        #else consider the new element as the current max
                foo_dict[e[0][0]] = e
        return foo_dict.values()
    
    >>> foo(somelist)
    [[['a'], [24], 3124, 1], [['c'], [24], 34, 1], [['b'], [24], 312, 1]]
    >>> foo(somelist,min)
    [[['a'], [24], 214, 1], [['c'], [24], 34, 1], [['b'], [24], 312, 1]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a list of 100 complex nested object, and you want to
Suppose I have a dictionary, and it's nested with dictionaries inside. I want to
Let's suppose I have below nested/multi-dim array: array( 'World'=>array( 'Asia'=>array( 'Japan'=>array( 'City'=>'Tokyo' ) )
Suppose I have a list of nodes which represent a nested set hierachy (examples
Suppose I have a list nested within a list and I have some function
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a bulleted list like this: * list item 1 * list
Suppose we have to use in some function deeply nested pointer very extensively: function
Suppose we have an iterator (an infinite one) that returns lists (or finite iterators),

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.