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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:44:53+00:00 2026-06-15T16:44:53+00:00

When appending longer statements to a list, I feel append becomes awkward to read.

  • 0

When appending longer statements to a list, I feel append becomes awkward to read. I would like a method that would work for dynamic list creation (i.e. don’t need to initialize with zeros first, etc.), but I cannot seem to come up with another way of doing what I want.

Example:

import math
mylist = list()
phi = [1,2,3,4] # lets pretend this is of unknown/varying lengths
i, num, radius = 0, 4, 6

while i < num:
    mylist.append(2*math.pi*radius*math.cos(phi[i]))
    i = i + 1

Though append works just fine, I feel it is less clear than:

mylist[i] = 2*math.pi*radius*math.cos(phi[i])

But this does not work, as that element does not exist in the list yet, yielding:

IndexError: list assignment index out of range

I could just assign the resulting value to temporary variable, and append that, but that seems ugly and inefficient.

  • 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-15T16:44:55+00:00Added an answer on June 15, 2026 at 4:44 pm

    In this particular case you could use a list comprehension:

    mylist = [2*math.pi*radius*math.cos(phi[i]) for i in range(num)]
    

    Or, if you’re doing this sort of computations a lot, you could move away from using lists and use NumPy instead:

    In [78]: import numpy as np
    
    In [79]: phi = np.array([1, 2, 3, 4])
    
    In [80]: radius = 6
    
    In [81]: 2 * np.pi * radius * np.cos(phi)
    Out[81]: array([ 20.36891706, -15.68836613, -37.32183785, -24.64178397])
    

    I find this last version to be the most aesthetically pleasing of all. For longer phi it will also be more performant than using lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes in my project that I would like to pass the
When appending to a file using Windows batch commands, how to append immediately after
Some claim that appending to immutable lists is more efficient. Is this true? How?
I am appending new data - $('#posts').append(data); But I want this new data to
try { // Create an appending file handler boolean append = true; FileHandler handler
I have a dynamically appearing div on a page. I would like to be
/* Program that reads a sequence of words from keyboard and prints the list
I'm using FF 3.6 and FireBug 1.5.0 my console.log statements are no longer appearing
I am dynamically appending and removing substring from NSString - At specific action I
I was having trouble appending object file in my program. I found this SO

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.