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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:43:10+00:00 2026-05-16T16:43:10+00:00

I have a function that is passed two values and then iterates over the

  • 0

I have a function that is passed two values and then iterates over the range of those values. The values can be passed in any order, so I need to find which one is the lowest first. I had the function written like this:

def myFunc(x, y):
    if x > y:
        min_val, max_val = y, x
    else:
        min_val, max_val = x, y
    for i in range(min_val, max_val):
    ...

But to save some screen space, I ended up changing it to:

def myFunc(x, y):
   min_val, max_val = sorted([x, y])
   for i in range(min_val, max_val):
   ...

How bad is this? Is there a better way that’s still one line?

  • 1 1 Answer
  • 1 View
  • 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-16T16:43:10+00:00Added an answer on May 16, 2026 at 4:43 pm

    Unless you need to microoptimise, I’d just to this

    def myFunc(x, y):
        for i in range(*sorted((x, y))):
            ...
    

    This is faster though

    def myFunc(x, y):
        for i in range(x,y) if x<y else range(y,x):
            ...
    

    minmax.py

    def f1(x, y):
        for i in range(min(x, y), max(x, y)):
            pass
    
    def f2(x, y):
        for i in range(*sorted((x, y))):
            pass
    
    def f3(x, y):
        for i in range(x, y) if x<y else range(y, x):
            pass
    
    def f4(x, y):
        if x>y:
            x,y = y,x
        for i in range(x, y):
            pass
    
    def f5(x, y):
        mn,mx = ((x, y), (y, x))[x>y]
        for i in range(x,y):
            pass
    

    benchmarks (f3 is fastest regardless of the order)

    $ python -m timeit -s"import minmax as mm" "mm.f1(1,2)"
    1000000 loops, best of 3: 1.93 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f2(1,2)"
    100000 loops, best of 3: 2.4 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f3(1,2)"
    1000000 loops, best of 3: 1.16 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f4(1,2)"
    100000 loops, best of 3: 1.2 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f5(1,2)"
    1000000 loops, best of 3: 1.58 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f1(2,1)"
    100000 loops, best of 3: 1.88 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f2(2,1)"
    100000 loops, best of 3: 2.39 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f3(2,1)"
    1000000 loops, best of 3: 1.18 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f4(2,1)"
    1000000 loops, best of 3: 1.25 usec per loop
    $ python -m timeit -s"import minmax as mm" "mm.f5(2,1)"
    1000000 loops, best of 3: 1.44 usec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that returns two values in a list. Both values need
I have a fairly complicated function that takes several double values that represent two
I have a function that creates zip files when passed an array of files
I have a JS function which is passed a string that a RegEx is
I have a class with defined functions that need to be passed as parameters.
I have a function that takes an input string and then runs the string
Okay, so I have two classes, call them A and B--in that order in
I have one method that opens a file and passes off to another function
Inside my Controller i have function that runs after user clicks on item, which
So I have function that formats a date to coerce to given enum DateType{CURRENT,

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.