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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:41:34+00:00 2026-05-26T21:41:34+00:00

Just an academic question. I’m curious what version of this code is better (faster)

  • 0

Just an academic question. I’m curious what version of this code is better (faster) in Python?

var = random.randint(1, 10)
# First version
if var in [2, 5, 9]:
  print "First: 2, 5 or 9"

# Second version
if var == 2 or number == 5 or number == 9:
  print "Second: 2, 5 or 9"

This is very simple example, but what if variable var is not a number, but string?

var = 'aaa'
# First version
if var in ['aaa', 'zzz', 'eee']:
  print "String"

What about some more complicated objects (not only numbers or string, but some class with very time-consuming comparison)?

And what happens inside Python compiler? I suppose if var in list is executed like:

for l in list:
  if l == var:
    print "String"

So in my opinion both versions in first example are the same (speed). Am I right?

  • 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-26T21:41:35+00:00Added an answer on May 26, 2026 at 9:41 pm

    Timing the code will show which is fastest. Use the timeit module for that:

    ~ $ python -m timeit --setup 'var=2' 'var in [2, 5, 9]'
    10000000 loops, best of 3: 0.0629 usec per loop
    ~ $ python -m timeit --setup 'var=5' 'var in [2, 5, 9]'
    10000000 loops, best of 3: 0.0946 usec per loop
    ~ $ python -m timeit --setup 'var=9' 'var in [2, 5, 9]'
    10000000 loops, best of 3: 0.117 usec per loop
    
    ~ $ python -m timeit --setup 'var=2' 'var == 2 or var==5 or var == 9'
    10000000 loops, best of 3: 0.0583 usec per loop
    ~ $ python -m timeit --setup 'var=5' 'var == 2 or var==5 or var == 9'
    10000000 loops, best of 3: 0.104 usec per loop
    ~ $ python -m timeit --setup 'var=9' 'var == 2 or var==5 or var == 9'
    10000000 loops, best of 3: 0.127 usec per loop
    

    If you want to improve your instincts on what Python is doing under-the-hood and knowing which code is fastest, start by disassembling code:

    def f(x):
        var = random.randint(1, 10)
        # First version
        if var in [2, 5, 9]:
          print "First: 2, 5 or 9"
    
        # Second version
        if var == 2 or number == 5 or number == 9:
          print "Second: 2, 5 or 9"
    
    import dis
    dis.dis(f)
    

    This will show that the this-or-this-or-that code does more steps than the list.__contains__ version.

    FWIW, you may want too consider using sets instead of a list. Their O(1) lookups tend to beat lists depending on key frequency (is the first element of the list the most likely match), and on how expensive the hash function is, and on the number of elements (sets scale up better than lists with their O(n) search:

    if var in {2, 5, 9}:
        ...
    

    On my machine, sets did not help for searching a small number of integer elements:

    ~ $ python -m timeit --setup 'var=2' 'var in {2, 5, 9}'
    1000000 loops, best of 3: 0.276 usec per loop
    ~ $ python -m timeit --setup 'var=5' 'var in {2, 5, 9}'
    1000000 loops, best of 3: 0.281 usec per loop
    ~ $ python -m timeit --setup 'var=9' 'var in {2, 5, 9}'
    1000000 loops, best of 3: 0.304 usec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is just an academic question (I would never do this in real code):
This question is mostly just out of academic interest. I started using YUI 3
This is some kind of academic question, I just wondering if it is possible
Just so it's known, this question is mostly academic, even though I tried to
Just found this out, so i am answering my own question :) Use a
This is more of an academic question about performance than a realistic 'what should
I have just gotten Visual Studio 2010 Pro Academic Version today with the MSDN
This is a bit more of an academic question. Indeed I am preparing for
This question is purely academic, because I'd never dream of doing this in real
Somewhat of an academic question, but I ran into this while writing some unit

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.