Two ways to insert items in sorted order

# Insert by maintaining sorted order from time import perf_counter A = [1, 4, 12, 33, 45, 37, 16, 13] insert_items = [6, 17, 32, 47, 19, 11, 7, 17, 38, 16, 7, 12, 33] A.sort() # Using bisect from bisect import bisect, insort B = A.copy() s = perf_counter() for insert_item in insert_items: insert_idx = bisect(B, insert_item) insort(B, insert_item, insert_idx) # no assignment required e = perf_counter() bisect_time = e-s print(B) # [1, 4, 6, 7, 11, 12, 13, 16, 17, 17, 19, 32, 33, 37, 45, 47] # Using numpy import numpy as np C = A.copy() C = np.array(C) s = perf_counter() for insert_item in insert_items: insert_idx = np.searchsorted(C, insert_item) C = np.insert(C, insert_idx, insert_item) # assignment required e = perf_counter() numpy_time = e-s print(C.tolist()) # [1, 4, 6, 7, 11, 12, 13, 16, 17, 17, 19, 32, 33, 37, 45, 47] print(B == C.tolist()) # True modules = ['bisect', 'numpy'] times = [bisect_time, numpy_time] speedup = (max(times) / min(times)) fastest_module_idx = times.index(min(times)) slowest_module_idx = [0, 1] slowest_module_idx.remove(fastest_module_idx) slowest_module_idx = slowest_module_idx[0] fastest_module = modules[fastest_module_idx] slowest_module = modules[slowest_module_idx] print('{fastest_module} is {speedup} times faster than {slowest_module} in maintaining sorted order.'.format( fastest_module = fastest_module.capitalize(), speedup = round(speedup, 2), slowest_module = slowest_module )) # Bisect is 31.19 times faster than numpy in maintaining sorted order.