Every line of 'create dictionary from two lists python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
18 def merge_list_dicts(list_dicts): 19 z = list_dicts[0].copy() # start with x's keys and values 20 for i in range(1, len(list_dicts)): 21 z.update(list_dicts[i]) # modifies z with y's keys and values & returns None 22 return z
12 def create_dict(src_dict, dest_dict, list_of_keys): 13 result = {} 14 tail = '' 15 for i in list_of_keys: 16 # 'KEY1, KEY2, KEY3' 17 if ',' in i: 18 list_of_i = i.split(', ') # ['KEY1', 'KEY2', 'KEY3'] 19 tail = ''.join(['[%s]' % j for j in list_of_i]) # '[KEY1][KEY2][KEY3]' 20 else: 21 tail = '[%s]' % i 22 k = src_dict + tail 23 k = re.sub(r'destination_ipv6_network', 'destination_network', k) 24 k = re.sub(r'destination_ipv4_network', 'destination_network', k) 25 k = re.sub(r'source_ipv6_network', 'source_network', k) 26 k = re.sub(r'source_ipv4_network', 'source_network', k) 27 v = dest_dict + tail 28 result[k] = v 29 30 return result
152 def list2dict(a_list, keyitem, alwayslist=False): 153 '''Return a dictionary with specified keyitem as key, others as values. 154 keyitem can be an index or a sequence of indexes. 155 For example: li=[['A','a',1], 156 ['B','a',2], 157 ['A','b',3]] 158 list2dict(li,0)---> {'A':[('a',1),('b',3)], 159 'B':('a',2)} 160 if alwayslist is True, values are always a list even there is only one item in it. 161 list2dict(li,0,True)---> {'A':[('a',1),('b',3)], 162 'B':[('a',2),]} 163 ''' 164 _dict = {} 165 for x in a_list: 166 if isinstance(keyitem, int): # single item as key 167 key = x[keyitem] 168 value = tuple(x[:keyitem] + x[keyitem + 1:]) 169 else: 170 key = tuple([x[i] for i in keyitem]) 171 value = tuple([x[i] for i in range(len(a_list)) if i not in keyitem]) 172 if len(value) == 1: # single value 173 value = value[0] 174 if key not in _dict: 175 if alwayslist: 176 _dict[key] = [value, ] 177 else: 178 _dict[key] = value 179 else: 180 current_value = _dict[key] 181 if not isinstance(current_value, list): 182 current_value = [current_value, ] 183 current_value.append(value) 184 _dict[key] = current_value 185 return _dict