Run 3 variables at once in a python for loop.
Question: For loop with multiple variables in python 2.7.

Hello,

I am not certain how to go about this, I have a function that goes to a site
and downloads a .csv file. It saves the .csv file in a particular format:
name_uniqueID_dataType.csv. here is the code

    
    
    import requests
    
    name = "name1"
    id = "id1" 
    dataType = "type1"
    
    
    def downloadData():
        URL = "http://www.website.com/data/%s" %name #downloads the file from the website. The last part of the URL is the name
        r = requests.get(URL)
        with open("data/%s_%s_%s.csv" %(name, id, dataType), "wb") as code: #create the file in the format name_id_dataType
            code.write(r.content)
    
    downloadData()
    

The code downloads the file and saves it perfectly fine. I want to run a for
loop on the function that takes those three variables each time. The variables
will be written as lists.

    
    
    name = ["name1", "name2"]
    id = ["id1", "id2"] 
    dataType = ["type1", "type2"]
    

There will be over 100 different items listed in each list with the same
amount of items in each variable. Is there any way to accomplish this using a
for loop in python 2.7. I have been doing research on this for the better part
of a day but I can't find a way to do it. Please note that I am new to python
and this is my first question. Any assistance or guidance would be greatly
appreciated.


Answer: [zip](https://docs.python.org/2/library/functions.html#zip) the lists and use
a for loop:

    
    
    def downloadData(n,i,d):
        for name, id, data in zip(n,i,d):
            URL = "http://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..

