Iterate through and print the keys of a dictionary instead of its values?
Question: I have a series of extremely long URLs with keys in the format of
`myurls21,1`, `myurls21,2`, etc. that I am breaking up into component parts
using the following statement:

    
    
    import concurrent.futures
    import urllib.request
    import json
    
    
    myurls2 = {}
    for x in range(1, 15):
        for y in range(1, 87):
    
            strvar1 = "%s" % (x)
            strvar2 = "%s" % (y)
    
            with open("C:\\Python33\\NASDAQ Stock Strings\\NASDAQ_Config_File_{}_{}.txt".format(x,y),"r") as f:
                myurls2[x,y] = f.read().replace('\n', '')
                #print("myurls_" + str(strvar1) + "_" + str(strvar2) + "=", myurls2[x,y])
                #print(myurls2[x,y])
    
    URLS = myurls2.values()
    
    # Retrieve a single page and report the url and contents
    def load_url(url, timeout):
        conn = urllib.request.urlopen(url, timeout=timeout)
        return conn.readall()
    
    
    # We can use a with statement to ensure threads are cleaned up promptly
    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        # Start the load operations and mark each future with its URL
        future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    
        c = 0
    
    
        for future in concurrent.futures.as_completed(future_to_url):
            url = future_to_url[future]
    
    
    
            a = ''
            b = ''
            c += 1
    
    
            for url in URLS:
                a = url.rsplit("=", 1)[-1]
                print(myurls2.keys())
    

How can I print the key of each URL being split into the log? I have tried
`print(myurls2.keys())`, but that prints all of the keys stored within the
dictionary (URLS is defined from the contents of the `myurls2` dictionary).


Answer: If you want to iterate over the keys _and_ values in a dictionary, you can do
for example:

    
    
    for key, url in myurls2.items():
        print(key)
        a = url.rsplit("=", 1)[-1]
    

* * *

If you want the keys for the urls you have passed to `concurrent`, refactor to
allow this:

    
    
    future_to_url = {executor.submit(load_url, url, 60): (key, url)
                     for key, url in myurls2.items()}
    

Now you can access it:

    
    
    for future in concurrent.futures.as_completed(future_to_url):
        key, url = future_to_url[future]
        # print key and process url
    

