Cheng's Blog

  • About Me
  • Archive
  • feeds

Posts match “ YouTube ” tag:

over 6 years ago

Python various ways to copy a list

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)
  • Python
  • video
  • YouTube
  • September 22, 2015 14:28
  • Permalink
  • Comments
 

Copyright © 2013 GuoCheng . Powered by Logdown.
Based on work at subtlepatterns.com.