tags:

views:

121

answers:

5

I've started teaching myself Python, and as an exercise I've set myself the task of generating lookup tables I need for another project. I need to generate a list of 256 elements in which each element is the value of math.sin(2pi/256). The problem is I don't know how to generate a list initialized to "dummy" values that I can then use a for loop to step through and assign the values of the sin function. Using list[] seems to create an "empty" list, but with no elements so I get a "list assignment index out of range" error in the loop. Is there a way to this other than explicitly creating a list declaration containing 256 elements all with "0" as a value? Thanks!

+5  A: 
my_list = [math.sin(2 * math.pi/256) for i in xrange(256)]
Bertrand Marron
+1, since this does produce, as its result, what the OP so explicitly says he wants (rather than something utterly different like the other equally-upvoted answer). But it's still absurd to do SO much work when `[math.sin(2 * math.pi/256)] * 256` would do the same job hundreds of times faster!-).
Alex Martelli
+6  A: 

Two answers have already shown you how to build your list at a single stroke, using the "list comprehension" (AKA "listcomp") construct.

To answer your specific question, though,

mylist = [None] * 256

is the simplest way to make a list with 256 items, all None, in case you want to fill it in later.

If you start with an empty list, call its .append(...) method to add each item at the end. A loop doing nothing but append on an initially-empty list is what normally gets replaced with a more concise listcomp.

Of course, for the task you actually state,

mylist = [math.sin(2 * math.pi/256)] * 256

would be by far the best approach -- no sense computing a sin 256 times when the argument's always the same (daringly assuming that what you say is what you mean;-).

Alex Martelli
Alex, is this Dummy Python Arrays for Dummies?
Peter Rowell
@Peter, the author of "Python for Dummies" is an old friend of mine and the book is pretty good indeed. This is just a simple attempt to explain a few of the "facts of life" about Python lists.
Alex Martelli
@Alex: *Facts of life?!* Are you saying there's a book about, *gasp!*, snake sex? Perhaps it even has a `list` of positions.
Peter Rowell
+1  A: 

I need to generate a list of 256 elements in which each element is the value of math.sin(2*math.pi/256)

To answer your question literally:

my_list=[math.sin(2*math.pi/256)]*256
unutbu
+1  A: 

You can also try:

l = []
for i in range(256):
    l.append(math.sin(2*math.pi/256))

This is an iterative for loop that keeps adding the same value to the end of the list 256 times

inspectorG4dget
A: 

Thanks for the help, everyone. I did make a mistake in the specification I posted for the question, in that the value of each element in the list needs to be the sin of the angle incremented by 2*pi/256 each time. The code that works for me in that case is:

li = [math.sin((2*math.pi/256)*i) for i in xrange(0,256)]
Bitrex
So accept answer that worked for you by clicking on tick sign on left margin.
TheMachineCharmer