I have several lists which I need to iterate over. I've got the zip function working very nicely, but to make my code more flexible I was wondering how you would use zip, but get the number of lists from a variable?
For example, rather than:
for 1,2,3 in zip(list_1,list_2,list3):
do something
We can do something more like:
i = 1
var = number of lists
zipall = zip(all_my_lists)
for i in range(1,var) in zip(zipall):
do something
This code isn't even close to working, I was just wanting to give an idea of the sort of thing I want to be able to do. Any tips would be very much appreciated. Thanks in advance.
Update
Thanks for the tips so far. It looks like using the * function might do the job for me. So far I'm trying the get the first half of my zip statement working:
args = [0,1]
for i in range(*args) in zip(outputlist[0],outputlist[1]):
print range(*args)
but line beginning "for" is giving me the following error:
TypeError: 'bool' object is not iterable
Any idea where I am going wrong? Thanks very much for the help so far.
First of all, the actual lists need to be specified, or be in another collection so that you can dynamically access them. Assuming you have a list of lists, you can easily do the following: