Quantcast
Channel: How do I clone a list so that it doesn't change unexpectedly after assignment? - Stack Overflow
Viewing all articles
Browse latest Browse all 117

Answer by fjemi for List changes unexpectedly after assignment. Why is this and how to prevent it?

$
0
0

The method to use depends on the contents of the list being copied. If the list contains nested dicts than deepcopy is the only method that works, otherwise most of the methods listed in the answers (slice, loop [for], copy, extend, combine, or unpack) will work and execute in similar time (except for loop and deepcopy, which preformed the worst).

Script

from random import randintfrom time import timeimport copyitem_count = 100000def copy_type(l1: list, l2: list):  if l1 == l2:    return 'shallow'  return 'deep'def run_time(start, end):  run = end - start  return int(run * 1000000)def list_combine(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = [] + l1  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'combine', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_extend(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = []  l2.extend(l1)  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'extend', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_unpack(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = [*l1]  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'unpack', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_deepcopy(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = copy.deepcopy(l1)  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'deepcopy', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_copy(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = list.copy(l1)  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'copy', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_slice(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = l1[:]  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'slice', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_loop(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = []  for i in range(len(l1)):    l2.append(l1[i])  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'loop', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}def list_list(data):  l1 = [data for i in range(item_count)]  start = time()  l2 = list(l1)  end = time()  if type(data) == dict:    l2[0]['test'].append(1)  elif type(data) == list:    l2.append(1)  return {'method': 'list()', 'copy_type': copy_type(l1, l2), 'time_µs': run_time(start, end)}if __name__ == '__main__':  list_type = [{'list[dict]': {'test': [1, 1]}},           {'list[list]': [1, 1]}]  store = []  for data in list_type:    key = list(data.keys())[0]    store.append({key: [list_unpack(data[key]), list_extend(data[key]),                 list_combine(data[key]), list_deepcopy(data[key]),                 list_copy(data[key]), list_slice(data[key]),                           list_loop(data[key])]})  print(store)

Results

[{"list[dict]": [  {"method": "unpack", "copy_type": "shallow", "time_µs": 56149},  {"method": "extend", "copy_type": "shallow", "time_µs": 52991},  {"method": "combine", "copy_type": "shallow", "time_µs": 53726},  {"method": "deepcopy", "copy_type": "deep", "time_µs": 2702616},  {"method": "copy", "copy_type": "shallow", "time_µs": 52204},  {"method": "slice", "copy_type": "shallow", "time_µs": 52223},  {"method": "loop", "copy_type": "shallow", "time_µs": 836928}]},{"list[list]": [  {"method": "unpack", "copy_type": "deep", "time_µs": 52313},  {"method": "extend", "copy_type": "deep", "time_µs": 52550},  {"method": "combine", "copy_type": "deep", "time_µs": 53203},  {"method": "deepcopy", "copy_type": "deep", "time_µs": 2608560},  {"method": "copy", "copy_type": "deep", "time_µs": 53210},  {"method": "slice", "copy_type": "deep", "time_µs": 52937},  {"method": "loop", "copy_type": "deep", "time_µs": 834774}]}]

Viewing all articles
Browse latest Browse all 117

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>