9 examples of 'how to merge two list in python' in Python

Every line of 'how to merge two list in 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
181def mergeList(list_of_list):
182 return list(itertools.chain.from_iterable(list_of_list))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

92def merge_lists(list_1, list_2, key, replace=False):
93 merged = {}
94 for item in list_1 + list_2:
95 item_key = item[key]
96 if item_key in merged:
97 if replace:
98 merged[item_key].update(item)
99 else:
100 merged[item_key].update(
101 item.items() + merged[item_key].items()
102 )
103 else:
104 merged[item_key] = item
105 return merged.values()
290def _merge_python_type(A, B):
291 a = _merge_order[A]
292 b = _merge_order[B]
293
294 if a >= b:
295 output = A
296 else:
297 output = B
298
299 if isinstance(output, str):
300 return output
301 else:
302 return output.__name__
15def merge(lh,rh):
16 res = []
17 i = 0
18 j = 0
19
20 while i<len(lh) and j<len(rh):
21 if lh[i] < rh[j]:
22 res.append(lh[i])
23 i+=1
24
25 else:
26 res.append(rh[j])
27 j+=1
28
29 if i<len(lh): res.extend(lh[i:])
30 if j<len(rh): res.extend(rh[j:])
31 return res
8def data_merge(obj_a, obj_b):
9 """merges obj_b into obj_a and return merged result
10
11 Args:
12 obj_a (dict) The "merge into dict"
13 obj_b (dict) The "merge from dict"
14
15 Returns:
16 dict: The obj_a with obj_b added to it
17 """
18 key = None
19 try:
20 if obj_a is None or isinstance(obj_a, (str, unicode, int, long, float)):
21 # border case for first run or if a is a primitive
22 obj_a = obj_b
23 elif isinstance(obj_a, list):
24 # lists can be only appended
25 if isinstance(obj_b, list):
26 # merge lists
27 obj_a.extend(obj_b)
28 else:
29 # append to list
30 obj_a.append(obj_b)
31 elif isinstance(obj_a, dict):
32 # dicts must be merged
33 if isinstance(obj_b, dict):
34 for key in obj_b:
35 if key in obj_a:
36 obj_a[key] = data_merge(obj_a[key], obj_b[key])
37 else:
38 obj_a[key] = obj_b[key]
39 else:
40 raise 'Cannot merge non-dict "%s" into dict "%s"' % (obj_b, obj_a)
41 else:
42 raise 'NOT IMPLEMENTED "%s" into "%s"' % (obj_b, obj_a)
43 except TypeError, exc:
44 raise 'TypeError "%s" in key "%s" when merging "%s" into "%s"' % (exc, key, obj_b, obj_a)
45 return obj_a
18def 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
17def _merge_dict(dict_1, dict_2):
18 new_dict = {}
19 for key in dict_1.keys():
20 if isinstance(dict_1[key], list):
21 new_dict[key] = dict_1[key]
22 else:
23 new_dict[key] = [dict_1[key]]
24
25 for key in dict_2.keys():
26 if key in new_dict:
27 if isinstance(dict_2[key], list):
28 new_dict[key].extend(dict_2[key])
29 else:
30 new_dict[key].append(dict_2[key])
31 else:
32 if isinstance(dict_2[key], list):
33 new_dict[key] = dict_2[key]
34 else:
35 new_dict[key] = [dict_2[key]]
36
37 return new_dict
55def mergeTwoLists2(self, l1, l2):
56 """
57 :param l1:
58 :param l2:
59 :return:
60
61 recursively
62 """
63 if not l1 or not l2:
64 return l1 or l2
65 if l1.val < l2.val:
66 l1.next = self.mergeTwoLists(l1.next, l2)
67 return l1
68 else:
69 l2.next = self.mergeTwoLists(l1, l2.next)
70 return l2
24def merge(a, b):
25 for key in b:
26 if key in a:
27 if isinstance(a[key], dict) and isinstance(b[key], dict):
28 merge(a[key], b[key])
29 else:
30 a[key] = b[key]
31 return a

Related snippets