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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:21:05+00:00 2026-05-13T22:21:05+00:00

I encounter the following small annoying dilemma over and over again in Python: Option

  • 0

I encounter the following small annoying dilemma over and over again in Python:

Option 1:

cleaner but slower(?) if called many times since a_list get re-created for each call of do_something()

def do_something():    
  a_list = ["any", "think", "whatever"]    
  # read something from a_list

Option 2:

Uglier but more efficient (spare the a_list creation all over again)

a_list = ["any", "think", "whatever"]    
def do_something():    
  # read something from a_list

What do you think?

  • 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-13T22:21:05+00:00Added an answer on May 13, 2026 at 10:21 pm

    What’s ugly about it?

    Are the contents of the list always constants, as in your example? If so: recent versions of Python (since 2.4) will optimise that by evaluating the constant expression and keeping the result but only if it’s a tuple. So you could change it to being a tuple. Or you could stop worrying about small things like that.

    Here’s a list of constants and a tuple of constants:

    >>> def afunc():
    ...    a = ['foo', 'bar', 'zot']
    ...    b = ('oof', 'rab', 'toz')
    ...    return
    ...
    >>> import dis; dis.dis(afunc)
      2           0 LOAD_CONST               1 ('foo')
                  3 LOAD_CONST               2 ('bar')
                  6 LOAD_CONST               3 ('zot')
                  9 BUILD_LIST               3
                 12 STORE_FAST               0 (a)
    
      3          15 LOAD_CONST               7 (('oof', 'rab', 'toz'))
                 18 STORE_FAST               1 (b)
    
      4          21 LOAD_CONST               0 (None)
                 24 RETURN_VALUE
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Encounter following lines of code, but couldn't understand it. What is this (/ ...
I often encounter the following scenario where I need to offer many different types
Under iOS 5, I encounter the following problem : the following code is called
I rarely use inheritance, but when I do I sometimes encounter the following problem.
I am getting the following debugger error when I encounter a breakpoint on the
When working with text in Flash, I often encounter the following problem: http://www.aino.se/media/i/subpixel.png This
While trying to start Apache Tomcat from eclipse, I encounter the following error: 18
I've encountered a behavior I did not expect when executing the following Python script
I often encounter the following error: res\drawable\my-image-48.png: Invalid file name: must contain only [a-z0-9_.]
I'm developing an eclipse plugin. When testing it, I sometimes encounter the following error:

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.