about 8 years ago

UPDATE: A video tutorial of this post has been created by webucator. They also offer a set of Python online training classes.

Thanks to this SO post, here are the ways to shallow and deep copy lists in Python:

Shallow Copy (from fastest to slowest)

new_list = old_list[:]
new_list = [] 
new_list.extend(old_list)
new_list = list(old_list)
import copy

new_list = copy.copy(old_list)
new_list = [i for i in old_list]
new_list = []

for i in old_list:
    new_list.append(i)

Deepcopy (the slowest)

import copy

new_list = copy.deepcopy(old_list)
← Linting React/JSX and ES6 Javascript with Eslint in Sublime Text 3 Converting React project from ES5 to ES6 →
 
comments powered by Disqus