Sunday, 2 June 2013

Python- How to generate new list from variable in a loop?

Python- How to generate new list from variable in a loop?

My actual example is more involved so I boiled the concept down to a simple example:
l = [1,2,3,4,5,6,7,8]
for number in l: calc = number*10 print calc
For each iteration of my loop, I end up with a variable (calc) I'd like to use to populate a new list. My actual process involves much more than multiplying the value by 10, so I'd like to be able to set the each value in the new list by this method. New code might look like this:
l = [1,2,3,4,5,6,7,8]
for number in l: calc = number*10 set calc as x'th entry in a new list called l2 (x = iteration cycle)
print l2
Then it would print the new list. [10,20,30,ect]
Thanks All,

No comments:

Post a Comment