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 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

I often encounter the following scenario where I need to offer many different types
In the following snippet, using XmlReader, when I encounter an element. I would like
I frequently encounter some definitions for Win32API structures (but not limited to it) that
I encountered the following ddl in a pl/sql script this morning: create index genuser.idx$$_0bdd0011
Has anyone encountered the following problem: I have IIS7 running on my computer. On
We've encountered the following issue. I like to use the following writing: SELECT Id,
I am debugging some code and have encountered the following SQL query (simplified version):
I encounter an error of the form: The model item passed into the dictionary
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,
I frequently encounter this situation in my VB6 applications Private Sub DoSomething On 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.