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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:10:37+00:00 2026-06-14T04:10:37+00:00

Every once in a while the following scenario turns up while I code in

  • 0

Every once in a while the following scenario turns up while I code in Python:

I iterate through a loop and find that I want to store the intermediate values of a variable in a list to be used later. Following is a very basic sample code:

for x in range(0,10):
    y = x*x
    temp_list.append(y)

The problem is temp_list has not been declared yet. So I usually navigate to the top of the loop and then add the following:

temp_list = []
for x in range(0,10):
    y = x*x
    temp_list.append(y)

While this may seem trivial, I was just wondering whether there is any pythonic way to create a list if it does not exist and then append a value to it, or else if it exists, just append?

  • 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-06-14T04:10:39+00:00Added an answer on June 14, 2026 at 4:10 am

    For your very base sample it can be done using list comprehension:

    l = [x*x for x in range(0, 10)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using Visual studio 2008. Every once in a while files that exist in
In this very simple code example, messages get lost every once in a while.
Suppose I want to run the following method foo() once every hour in Grails:
I have a .sql script file that I want to run once every 24
Following exception is thrown every once in a while and it shows up in
My activity plays a specific sound effect once every second. The following code works,
Every once in a while my app would throw the following error: Missing template
We get the following every once in a while. We cannot recreate this error.
The following code breaks once every 36 hours at a random time. The correctness
Every once and a while I get this error in IE when making an

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.