Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new_list = old_list[:]
- new_list = list(old_list)
- import copy
- new_list = copy.copy(old_list)
- import copy
- new_list = copy.deepcopy(old_list)
- import copy
- class Foo(object):
- def __init__(self, val):
- self.val = val
- def __repr__(self):
- return str(self.val)
- foo = Foo(1)
- a = ['foo', foo]
- b = a[:]
- c = list(a)
- d = copy.copy(a)
- e = copy.deepcopy(a)
- # edit orignal list and instance
- a.append('baz')
- foo.val = 5
- print('original: %rn slice: %rn list(): %rn copy: %rn deepcopy: %r'
- % (a, b, c, d, e))
- original: ['foo', 5, 'baz']
- slice: ['foo', 5]
- list(): ['foo', 5]
- copy: ['foo', 5]
- deepcopy: ['foo', 1]
- from copy import deepcopy
- class old_class:
- def __init__(self):
- self.blah = 'blah'
- class new_class(object):
- def __init__(self):
- self.blah = 'blah'
- dignore = {str: None, unicode: None, int: None, type(None): None}
- def Copy(obj, use_deepcopy=True):
- t = type(obj)
- if t in (list, tuple):
- if t == tuple:
- # Convert to a list if a tuple to
- # allow assigning to when copying
- is_tuple = True
- obj = list(obj)
- else:
- # Otherwise just do a quick slice copy
- obj = obj[:]
- is_tuple = False
- # Copy each item recursively
- for x in xrange(len(obj)):
- if type(obj[x]) in dignore:
- continue
- obj[x] = Copy(obj[x], use_deepcopy)
- if is_tuple:
- # Convert back into a tuple again
- obj = tuple(obj)
- elif t == dict:
- # Use the fast shallow dict copy() method and copy any
- # values which aren't immutable (like lists, dicts etc)
- obj = obj.copy()
- for k in obj:
- if type(obj[k]) in dignore:
- continue
- obj[k] = Copy(obj[k], use_deepcopy)
- elif t in dignore:
- # Numeric or string/unicode?
- # It's immutable, so ignore it!
- pass
- elif use_deepcopy:
- obj = deepcopy(obj)
- return obj
- if __name__ == '__main__':
- import copy
- from time import time
- num_times = 100000
- L = [None, 'blah', 1, 543.4532,
- ['foo'], ('bar',), {'blah': 'blah'},
- old_class(), new_class()]
- t = time()
- for i in xrange(num_times):
- Copy(L)
- print 'Custom Copy:', time()-t
- t = time()
- for i in xrange(num_times):
- Copy(L, use_deepcopy=False)
- print 'Custom Copy Only Copying Lists/Tuples/Dicts (no classes):', time()-t
- t = time()
- for i in xrange(num_times):
- copy.copy(L)
- print 'copy.copy:', time()-t
- t = time()
- for i in xrange(num_times):
- copy.deepcopy(L)
- print 'copy.deepcopy:', time()-t
- t = time()
- for i in xrange(num_times):
- L[:]
- print 'list slicing [:]:', time()-t
- t = time()
- for i in xrange(num_times):
- list(L)
- print 'list(L):', time()-t
- t = time()
- for i in xrange(num_times):
- [i for i in L]
- print 'list expression(L):', time()-t
- t = time()
- for i in xrange(num_times):
- a = []
- a.extend(L)
- print 'list extend:', time()-t
- t = time()
- for i in xrange(num_times):
- a = []
- for y in L:
- a.append(y)
- print 'list append:', time()-t
- t = time()
- for i in xrange(num_times):
- a = []
- a.extend(i for i in L)
- print 'generator expression extend:', time()-t
- a_copy = a_list.copy()
- a_copy = a_list[:]
- a_copy = a_list[:]
- a_copy = list(a_list)
- >>> timeit
- >>> l = range(20)
- >>> min(timeit.repeat(lambda: l[:]))
- 0.30504298210144043
- >>> min(timeit.repeat(lambda: list(l)))
- 0.40698814392089844
- a_copy = a_list.copy()
- >>> import timeit
- >>> l = list(range(20))
- >>> min(timeit.repeat(lambda: l[:]))
- 0.38448613602668047
- >>> min(timeit.repeat(lambda: list(l)))
- 0.6309100328944623
- >>> min(timeit.repeat(lambda: l.copy()))
- 0.38122922903858125
- >>> l = [[], [], []]
- >>> l_copy = l[:]
- >>> l_copy
- [[], [], []]
- >>> l_copy[0].append('foo')
- >>> l_copy
- [['foo'], [], []]
- >>> l
- [['foo'], [], []]
- import copy
- a_deep_copy = copy.deepcopy(a_list)
- >>> import copy
- >>> l
- [['foo'], [], []]
- >>> l_deep_copy = copy.deepcopy(l)
- >>> l_deep_copy[0].pop()
- 'foo'
- >>> l_deep_copy
- [[], [], []]
- >>> l
- [['foo'], [], []]
- problematic_deep_copy = eval(repr(a_list))
- >>> import timeit
- >>> import copy
- >>> l = range(10)
- >>> min(timeit.repeat(lambda: copy.deepcopy(l)))
- 27.55826997756958
- >>> min(timeit.repeat(lambda: eval(repr(l))))
- 29.04534101486206
- >>> import timeit
- >>> import copy
- >>> l = list(range(10))
- >>> min(timeit.repeat(lambda: copy.deepcopy(l)))
- 16.84255409205798
- >>> min(timeit.repeat(lambda: eval(repr(l))))
- 34.813894678023644
- new_list = list(my_list) # or my_list[:], but I prefer this syntax
- # is simply a shorter way of:
- new_list = [element for element in my_list]
- import copy
- # each element must have __copy__ defined for this...
- new_list = [copy.copy(element) for element in my_list]
- import copy
- # each element must have __deepcopy__ defined for this...
- new_list = copy.deepcopy(my_list)
- >>> a = [1,2]
- >>> b = a[:]
- >>> a += [3]
- >>> a
- [1, 2, 3]
- >>> b
- [1, 2]
- >>>
- new_list = eval(repr(old_list))
- old_list = [[0 for j in range(y)] for i in range(x)] # initialize (x,y) nested list
- # assign a copy of old_list to new list without them pointing to the same list object
- new_list = eval(repr(old_list))
- # make a change to new_list
- for j in range(y):
- for i in range(x):
- new_list[i][j] += 1
- >>> new_list
- [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
- >>> old_list
- [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
- METHOD TIME TAKEN
- b = a[:] 6.468942025996512 #Python2 winner
- b = a.copy() 6.986593422974693 #Python3 "slice equivalent"
- b = []; b.extend(a) 7.309216841997113
- b = a[0:len(a)] 10.916740721993847
- *b, = a 11.046738261007704
- b = list(a) 11.761539687984623
- b = [i for i in a] 24.66165203397395
- b = copy.copy(a) 30.853400873980718
- b = []
- for item in a:
- b.append(item) 48.19176080400939
- import timeit
- COUNT = 50000000
- print("Array duplicating. Tests run", COUNT, "times")
- setup = 'a = [0,1,2,3,4,5,6,7,8,9]; import copy'
- print("b = list(a)tt", timeit.timeit(stmt='b = list(a)', setup=setup, number=COUNT))
- print("b = copy.copy(a)tt", timeit.timeit(stmt='b = copy.copy(a)', setup=setup, number=COUNT))
- print("b = a.copy()tt", timeit.timeit(stmt='b = a.copy()', setup=setup, number=COUNT))
- print("b = a[:]tt", timeit.timeit(stmt='b = a[:]', setup=setup, number=COUNT))
- print("b = a[0:len(a)]t", timeit.timeit(stmt='b = a[0:len(a)]', setup=setup, number=COUNT))
- print("*b, = at", timeit.timeit(stmt='*b, = a', setup=setup, number=COUNT))
- print("b = []; b.extend(a)t", timeit.timeit(stmt='b = []; b.extend(a)', setup=setup, number=COUNT))
- print("b = []nfor item in a: b.append(item)t", timeit.timeit(stmt='b = []nfor item in a: b.append(item)', setup=setup, number=COUNT))
- print("b = [i for i in a]t", timeit.timeit(stmt='b = [i for i in a]', setup=setup, number=COUNT))
- a=[1,2,3]
- b=a
- list_1=['01','98']
- list_2=[['01','98']]
- copy=list_1
- The id() function shows us that both variables point to the same list object, i.e. they share this object.
- print(id(copy))
- print(id(list_1))
- 4329485320
- 4329485320
- copy[0]="modify"
- print(copy)
- print(list_1)
- ['modify', '98']
- ['modify', '98']
- copy_1=list_1[:]
- print(id(copy_1))
- print(id(list_1))
- 4338792136
- 4338791432
- copy_1[0]="modify"
- print(list_1)
- print(copy_1)
- ['01', '98']
- ['modify', '98']
- copy_2=list_2[:]
- print(id((list_2)),id(copy_2))
- 4330403592 4330403528
- copy_2[0][1]="modify"
- print(list_2,copy_2)
- [['01', 'modify']] [['01', 'modify']]
- copy_2=list_2[:]
- print(id(copy_2[0]))
- print(id(list_2[0]))
- 4329485832
- 4329485832
- from copy import deepcopy
- deep=deepcopy(list_2)
- print(id((list_2)),id(deep))
- 4322146056 4322148040
- print(id(deep[0]))
- print(id(list_2[0]))
- 4322145992
- 4322145800
- deep[0][1]="modify"
- print(list_2,deep)
- [['01', '98']] [['01', 'modify']]
- a = {'par' : [1,21,3], 'sar' : [5,6,8]}
- b = a
- c = a.copy()
- a['har'] = [1,2,3]
- a
- Out[14]: {'har': [1, 2, 3], 'par': [1, 21, 3], 'sar': [5, 6, 8]}
- b
- Out[15]: {'har': [1, 2, 3], 'par': [1, 21, 3], 'sar': [5, 6, 8]}
- c
- Out[16]: {'par': [1, 21, 3], 'sar': [5, 6, 8]}
- new_list = my_list[:]
- new_list = my_list * 1 #Solution 1 when you are not using nested lists
- import copy
- new_list = copy.deepcopy(my_list) #Solution 2 when you are using nested lists
- new_list = my_list[:]
- >>> a = range(5)
- >>> b = a*1
- >>> a,b
- ([0, 1, 2, 3, 4], [0, 1, 2, 3, 4])
- >>> a[2] = 55
- >>> a,b
- ([0, 1, 55, 3, 4], [0, 1, 2, 3, 4])
- >>> from copy import deepcopy
- >>> a = [range(i,i+4) for i in range(3)]
- >>> a
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
- >>> b = a*1
- >>> c = deepcopy(a)
- >>> for i in (a, b, c): print i
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
- >>> a[2].append('99')
- >>> for i in (a, b, c): print i
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5, 99]]
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5, 99]] #Solution#1 didn't work in nested list
- [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]] #Solution #2 - DeepCopy worked in nested list
- newlist=list(oldlist)
Add Comment
Please, Sign In to add comment