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

  • Home
  • SEARCH
  • 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 3948454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:22:36+00:00 2026-05-20T01:22:36+00:00

I have a problem to get the highest Value in a dynamic List of

  • 0

I have a problem to get the highest Value in a dynamic List of Lists of Tuples.
The List can looks like this:

adymlist = [[('name1',1)],[('name2',2),('name3',1), ...('name10', 20)], ...,[('name m',int),..]]

Now I loop through the List to get the highest Value (integer):

total = {}
y=0 
while y < len(adymlist):
    if len(adymlist) == 1:
         #has the List only 1 Element -> save it in total 
         total[adymlist[y][0][0]] = adymlist[y][0][1]
         y += 1
    else:
         # here is the problem
         # iterate through each lists to get the highest Value
         # and you dont know how long this list can be
         # safe the highest Value in total f.e. total = {'name1':1,'name10':20, ..}

I tried a lot to get the maximum Value but I found no conclusion to my problem. I know i must loop through each Tuple in the List and compare it with the next one but it dont know how to code it correct.

Also I can use the function max() but it doesnt work with strings and integers. f.e.
a = [ ('a',5),('z',1)] -> result is max(a) ---> ('z',1) obv 5 > 1 but z > a so I tried to expand the max function with max(a, key=int) but I get an Type Error.

Hope you can understand what I want 😉

UPDATE


Thanks so far.

If I use itertools.chain(*adymlist) and max(flatlist, key=lambda x: x[1])

I will get an exception like : max_word = max(flatlist, key=lambda x: x[1])
TypeError: ‘int’ object is unsubscriptable

BUT If I use itertools.chain(adymlist) it works fine. But I dont know how to summate all integers from each Tuple of the List. I need your help to figure it out.

Otherwise I wrote a workaround for itertools.chain(*adymlist) to get the sum of all integers and the highest integer in that list.

chain = itertools.chain(*adymlist)
flatlist = list(chain)
# flatlist = string, integer, string, integer, ...
max_count = max(flatlist[1:len(flatlist):2])
total_count = sum(flatlist[1:len(flatlist):2])
# index of highest integer
idx = flatlist.index(next((n for n in flatlist if n == max_count)))
max_keyword = flatlist[idx-1]

It still does what I want, but isn’t it to dirty?

  • 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-20T01:22:36+00:00Added an answer on May 20, 2026 at 1:22 am

    To clarify, looks like you’ve got a list of lists of tuples. It doesn’t look like we care about what list they are in, so we can simplify this to two steps

    • Flatten the list of lists to a list of tuples
    • Find the max value

    The first part can be accomplished via itertools.chain (see e.g., Flattening a shallow list in Python)

    The second can be solved through max, you have the right idea, but you should be passing in a function rather than the type you want. This function needs to return the value you’ve keyed on, in this case ,the second part of the tuple

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

    Correction

    I re-read your question – are you looking for the max value in each sub-list? If this is the case, then only the second part is applicable. Simply iterate over your list for each list

    A bit more pythonic than what you currently have would like

    output = [] 
    for lst in lists:
       output.append( max(flatlist, key=lambda x: x[1]) )
    

    or

    map(lambda x:  max(x, key=lambda y: y[1]) , lists)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have one interesting problem. I must parse mail body (regular expression), get some
I have a seemingly simple problem though i am unable to get my head
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I have a problem with a SQL statement: Using this select a.id as ID,
I have been having some problems trying to get my PHP running. When I
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem when I try insert some data to Informix TEXT column via

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.