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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:50:05+00:00 2026-05-13T05:50:05+00:00

This code seems to smell: result = None for item in list: if result

  • 0

This code seems to smell:

result = None
for item in list:
    if result is None:
        result = item.foo(args)
    else:
        if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)):
            result = item.foo(args)

The smelliest part is the utility of ‘result’. Would anyone be kind enough sniff it for me?

  • 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-13T05:50:06+00:00Added an answer on May 13, 2026 at 5:50 am
    L = list # 'list' is a poor variable name, use something else
    result = min((n.foo(args) for n in L),
                 key=lambda x: ClassFred.objects.get(arg1=x))
    # if you don't have to use arg1 as a named parameter:
    result = min((n.foo(args) for n in L), key=ClassFred.objects.get)
    

    The min function compares the given items and returns the minimum one (of course :P). What isn’t obvious at first is you can control what value is used to compare them, this is the ‘key’ parameter.

    >>> L = [-2, -1, 3]
    >>> min(L)
    -2
    >>> min(L, key=abs)
    -1
    

    The key function computes the “comparison key”, and that is what is used to compare. The default key function is identity, where the comparison key for an item is the item itself.

    >>> def identity(x):
    ...   return x
    >>> min(L, key=identity)
    -2
    

    Another example:

    >>> min("0000", "11", "222", "3")
    "0000" # lexicographical minimum
    >>> min("0000", "11", "222", "3", key=len)
    "3"
    

    Your code above is using item.foo(args) as values, where item comes from your list; but the result of passing that through ClassFred.objects.get(arg1=..) is used to compare. This means that construct is your key function:

    values = (n.foo(args) for n in L) # this is a generator expression
    # it is similar to a list comprehension, but doesn't compute or store
    # everything immediately
    
    def keyfunc(x):
      return ClassFred.objects.get(arg1=x)
    
    result = min(values, key=keyfunc)
    

    My code at the top just puts this together in one statement.

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

Sidebar

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.