I was wondering if there was a better way of writing this...I have a CSV file (about 6640 lines). After reading through the file, the values for the DFN_Network are stored in a list. After that the values of the DFN_Network are matched with their corresponding existence in the values of the dictionary all_dictionary. After that I create a new dictionary that matches k (line number) and the list of numbers where is occurs in the all_dictionary.h is used to make the exact match for the number case.len(DFN_Network) = 2720len(all_dictionary) = keys (1000)len(all_dictionary) = values (6000)Currently to iterate through the above it takes 192secs. Note this is only for 1,000 keys, I am planning to have over 25,000 keys.        while k in range(len(DFN_Network)):            listed = []            for q in range(len(all_dictionary.values())):                for w in range(len(all_dictionary.values()[q])):                    h = '^' + str(k) + '$'                    match = re.search(h, (all_dictionary.values()[q][w]))                    if match:                        abc = all_dictionary.keys()[q]                        listed.append(abc)            diction[k] = listed            k += 1DFN_Network = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84']all_dictionary = {'24': ['77', '78', '23', '57'], '25': ['33', '79', '80', '6', '81'], '26': ['43', '59', '26', '72'], '27': ['34', '81', '9', '74'], '20': ['54', '71', '24', '70', '48', '67'], '21': ['20', '39', '44', '72', '31', '73', '74', '8'], '22': ['68', '25', '71', '53'], '23': ['15', '76', '75', '4', '69'], '28': ['76', '14', '45', '40', '82'], '29': ['11', '84', '83', '61'], '1': ['2', '3', '4', '1'], '3': ['10', '11', '12', '13'], '2': ['6', '5', '7', '8', '9'], '5': ['19', '20', '7', '18'], '4': ['14', '15', '16', '17'], '7': ['26', '27', '28', '29', '30', '31'], '6': ['22', '23', '21', '24', '25'], '9': ['39', '19', '38', '37', '40', '41'], '8': ['33', '34', '35', '36', '32'], '11': ['13', '47', '48', '46'], '10': ['17', '42', '43', '44', '41', '45'], '13': ['52', '53', '54', '55', '28'], '12': ['36', '49', '50', '51'], '15': ['27', '59', '42', '60'], '14': ['2', '56', '57', '22', '58'], '17': ['51', '65', '62', '64'], '16': ['12', '61', '62', '63', '47'], '19': ['68', '52', '60', '16', '69', '3', '58'], '18': ['50', '66', '29', '55', '67', '63', '65'], '30': ['66', '49', '35', '73', '30']}diction = {0: [], 1: ['1'], 2: ['1', '14'], 3: ['1', '19'], 4: ['23', '1'], 5: ['2'], 6: ['25', '2'], 7: ['2', '5'], 8: ['21', '2'], 9: ['27', '2'], 10: ['3'], 11: ['29', '3'], 12: ['3', '16'], 13: ['3', '11'], 14: ['28', '4'], 15: ['23', '4'], 16: ['4', '19'], 17: ['4', '10'], 18: ['5'], 19: ['5', '9'], 20: ['21', '5'], 21: ['6'], 22: ['6', '14'], 23: ['24', '6'], 24: ['20', '6'], 25: ['22', '6'], 26: ['26', '7'], 27: ['7', '15'], 28: ['7', '13'], 29: ['7', '18'], 30: ['7', '30'], 31: ['21', '7'], 32: ['8'], 33: ['25', '8'], 34: ['27', '8'], 35: ['8', '30'], 36: ['8', '12'], 37: ['9'], 38: ['9'], 39: ['21', '9'], 40: ['28', '9'], 41: ['9', '10'], 42: ['10', '15'], 43: ['26', '10'], 44: ['21', '10'], 45: ['28', '10'], 46: ['11'], 47: ['11', '16'], 48: ['20', '11'], 49: ['12', '30'], 50: ['12', '18'], 51: ['12', '17'], 52: ['13', '19'], 53: ['22', '13'], 54: ['20', '13'], 55: ['13', '18'], 56: ['14'], 57: ['24', '14'], 58: ['14', '19'], 59: ['26', '15'], 60: ['15', '19'], 61: ['29', '16'], 62: ['17', '16'], 63: ['16', '18'], 64: ['17'], 65: ['17', '18'], 66: ['18', '30'], 67: ['20', '18'], 68: ['22', '19'], 69: ['23', '19'], 70: ['20'], 71: ['20', '22'], 72: ['26', '21'], 73: ['21', '30'], 74: ['27', '21'], 75: ['23'], 76: ['23', '28'], 77: ['24'], 78: ['24'], 79: ['25'], 80: ['25'], 81: ['25', '27'], 82: ['28'], 83: ['29']}So lets take diction.key() = 79 exists in all_dictionary.itemvalues() for key() = 25 ONLY.So lets take diction.key() = 81 exists in all_dictionary.itemvalues()  for key() = 25 & 27 ONLY.ADDITIONAL DESCRIPTION:Real situation explanation:I have a rectangle, this rectangle is divided into small polygons of 6 sides each (Keys in All_dictionary)These polygons are divided by lines (DFN_Network)I am trying to match the line shared between the polygons {DFN_Network : [Keys in All_dictionary]}Hence, every line will be an entry in the diction. And i need it this way because later on in the code, i match the  Keys in All_dictionary to their corresponding names
Matching lines in a CSV file with items in a dictionary
python;python 2.7;regex;time limit exceeded;dictionary
Your original algorithm is very inefficient and also error prone as mentioned with dictionaries being unordered and slow when converting keys and values to lists. Though I would not go this approach (see further down for a better example), here are some things about your current code.First, you are getting all_dictionary.values() many times! Once for each DNF_Network iteration times the number of values in the dictionary times the number of numbers in the value list!!! WOW! This is unnecessary and should have been moved outside loops, only retrieved once. The same thing goes for all_dictionary.keys(), every time a match is found, it will have to generate a new list of keys!Next you are regular expressions which are slower than string comparisons. I don't see you needing this in your example.In your description, you said you were using the values read from a file into a list DNF_Network but I do not see in your example code you using any value from that list. All I see you using the index of the DNF_Network list and not the the value at that index. If the DNF_Network list has values [2,5,8,20], then you would only be looking in all_dictionary for 0,1,2,3. So something doesn't make sense there.Here is what I would do which should be simple and efficient. Though if performance is important, you should profile the code to see where the bottlenecks are.# fill a default dictionary with keys from DNF_Network with empty lists as values. We could use collections defaultdict but this is to ensure that the dictionary has all values represented from DNF_Network in case all_dictionary does not contain it.DFN_network_dict = dict([(i,[]) for i in DFN_Network])for key, value in all_dictionary.iteritems(): # for Python 2.x otherwise use .items    for v in value:        try:            DFN_network_dict[v].append(key)        except KeyError:            # looks like this value isn't in DFN_Network, ignore it            pass