zip the lists and use a for loop:
def downloadData[n,i,d]:
for name, id, data in zip[n,i,d]:
URL = "//www.website.com/data/{}".format[name] #downloads the file from the website. The last part of the URL is the name
r = requests.get[URL]
with open["data/{}_{}_{}.csv".format[name, id, data], "wb"] as code: #create the file in the format name_id_dataType
code.write[r.content]
Then pass the lists to your function when calling:
names = ["name1", "name2"]
ids = ["id1", "id2"]
dtypes = ["type1", "type2"]
downloadData[names, ids, dtypes]
zip will group your elements by index:
In [1]: names = ["name1", "name2"]
In [2]: ids = ["id1", "id2"]
In [3]: dtypes = ["type1", "type2"]
In [4]: zip[names,ids,dtypes]
Out[4]: [['name1', 'id1', 'type1'], ['name2', 'id2', 'type2']]
So the first iteration name,id and data will be ['name1', 'id1', 'type1']
and so on..
Programming is done to solve problems more effectively. Python is no different and aims to solve diverse problems with appropriate solutions. For loop with multiple variable in python is a way to iterate through arrays of data items but before understanding how multiple variables can be ingested in a loop, we must first understand why the need of the for loop with multiple variables in python.
Let’s take a look at a few examples to understand more
Here a single list is iterated to display the values.
lst1 = [1,2,3,4,5]
for x in lst1:
print [x]
import itertools
lst1 = [1,2,3,4,5]
lst2=["banana","apple","mango","berry"]
lst3=["black","red"]
for [a] in zip[lst1, lst2, lst3]:
print [a]
- Output:
- zip method
- output:
- multiple variables through a single list
- output:
- conclusion
Output:
But suppose you want to add functionality to each list separately, that would be a hassle because the list of tuples is returned. for loop with two variables in python is a necessity that needs to be considered.
zip method
A solution one might reach is to use a zip method that allows lists to run parallel to each other.
import itertools
lst1 = [1,2,3,4,5]
lst2=["banana","apple","mango","berry"]
lst3=["black","red"]
for [a, b, c] in zip[lst1, lst2, lst3]:
print [a, b, c]
output:
Note how the loop stopped when the shortest list runs out of values. This is due to the default nature of the function to find the shortest list and terminate when all the values are iterated.
You can control that through the longest property of the zip method through for loop’s multiple index.
import itertools
lst1 = [1,2,3,4,5]
lst2=["banana","apple","mango","berry"]
lst3=["black","red"]
for [a, b, c] in itertools.zip_longest[lst1, lst2, lst2]:
print [a, b, c]
multiple variables through a single list
Multiple variables in for loops can have another unique use. iterate through a list of lists, in this case, iterate over a list of tuples. You can remove duplicates from a list in Python with For Loop.
tuple_list = [[1,2,3], [4,5,6], [7,8,9]]
for triple in tuple_list:
print[triple]
output:
conclusion
Looping through multiple lists simultaneously is an essential part of the structure. Writing nested loops or multiple statements to traverse through various lists can be hectic. Simpler functionality of methods such as zip allows the programs to be much easier to cope with.