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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:22:32+00:00 2026-05-28T08:22:32+00:00

def greatest(values): value_generator = (v for k,v in values) max_value = max(value_generator) return (k

  • 0
def greatest(values):
    value_generator = (v for k,v in values)
    max_value = max(value_generator)
    return (k for k,v in values if v == max_value)

sample_data = ( ('id1', 3), ('id2', 5), ('id3', 5) )
items = list( greatest(sample_data) ) # Should produce ['id2', 'id3']

MapReduce anyone?

  • 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-28T08:22:33+00:00Added an answer on May 28, 2026 at 8:22 am

    In fact, according to my tests, your version of greatest is faster — by a bit, anyway:

    >>> def greatest_orig(values):
    ...     value_generator = (v for k,v in values)
    ...     max_value = max(value_generator)
    ...     return (k for k,v in values if v == max_value)
    ... 
    >>> def greatest_max_key(values):
    ...     max_value = max(values, key=itemgetter(1))[1]
    ...     return (k for k,v in values if v == max_value)
    ... 
    >>> sample_data = tuple(('id' + str(i), random.randrange(0, 1000)) for i in range(10000))
    >>> list(greatest_orig(sample_data)) == list(greatest_max_key(sample_data))
    True
    >>> %timeit list(greatest_orig(sample_data))
    1000 loops, best of 3: 1.67 ms per loop
    >>> %timeit list(greatest_max_key(sample_data))
    1000 loops, best of 3: 1.74 ms per loop
    

    Of course, if you don’t like assigning your generator to a name, you can just pass the generator to max directly — way more readable than max(values, key=itemgetter(1))[1], IMHO:

    >>> def greatest_max_iter(values):
    ...     max_value = max((v for k, v in values))
    ...     return (k for k, v in values if v == max_value)
    ...                                                
    >>> list(greatest_orig(sample_data)) == list(greatest_max_iter(sample_data))
    True
    >>> %timeit list(greatest_max_iter(sample_data))
    1000 loops, best of 3: 1.67 ms per loop
    

    Python allows you to omit the outer parens when doing things like this:

    >>> def greatest_max_iter(values):
    ...     max_value = max(v for k, v in values)
    ...     return (k for k, v in values if v == max_value)
    ... 
    

    But for reasons I don’t understand, doing it that way is slightly slower:

    >>> %timeit list(greatest_max_iter(sample_data))
    1000 loops, best of 3: 1.69 ms per loop
    

    These are true micro-optimizations, unlikely to matter much. But I think that readability favors max(v for k, v in values) or max((v for k, v in values)) over max(values, key=itemgetter(1))[1].

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

Sidebar

Related Questions

def makecounter(): return collections.defaultdict(int) class RankedIndex(object): def __init__(self): self._inverted_index = collections.defaultdict(list) self._documents = []
def common_elements(list1, list2): Return a list containing the elements which are in both list1
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do
def foo(**args): for k, v in args.items(): print type(k), type(v) for k, v in
def foo f = Proc.new { return return from foo from inside proc }
def applejuice(q): print THE FUNCTION NAME! It should result in applejuice as a string.
def isBig(x): if x > 4: return 'apple' else: return 'orange' This works: if
def fun1(a): for i in range(len(a)): a[i] = a[i] * a[i] return a

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.