There are several options...
List comprehensions
Use list comprehension, if short enough:
new_list = [number * 10 for number in old_list]
map()
You could also use map()
, if the function exists before (or you will eg. use lambda
):
def my_func(value):
return value * 10
new_list = map(my_func, old_list)
Be aware, that in Python 3.x map()
does not return a list (so you would need to do something like this: new_list = list(map(my_func, old_list))
).
Filling other list using simple for ... in
loop
Alternatively you could use simple loop - it is still valid and Pythonic:
new_list = []
for item in old_list:
new_list.append(item * 10)
Generators
Sometimes, if you have a lot of processing (as you said you have), you want to perform it lazily, when requested, or just the result may be too big, you may wish to use generators. Generators remember the way to generate next element and forget whatever happened before (I am simplifying), so you can iterate through them once (but you can also explicitly create eg. list that stores all the results).
In your case, if this is only for printing, you could use this:
def process_list(old_list):
for item in old_list:
new_item = ... # lots of processing - item into new_item
yield new_item
And then print it:
for new_item in process_list(old_list):
print(new_item)
More on generators you can find in Python's wiki: http://wiki.python.org/moin/Generators
Accessing "iteration number"
But if your question is more about how to retrieve the number of iteration, take a look at enumerate()
:
for index, item in enumerate(old_list):
print('Item at index %r is %r' % (index, item))