shallow_deep_copy.py

# =========================================================
#
# =========================================================

import copy

print('--------------------------------------------------')

a = ['a','b','c','d']

b = [1,2,3,a,5,6,7,8]

# -- shallow copy

x = copy.copy(b)

print('original lists')
print('shallow b = {}'.format(b))
print('shallow x = {}'.format(x))

print('delete b[0]')
del(b[0])

print('shallow b = {}'.format(b))
print('shallow x = {}'.format(x))

print('delete a[1]')
del(a[1])

print('shallow b = {}'.format(b))
print('shallow x = {}'.format(x))

print('--------------------------------------------------')

a = ['a','b','c','d']

b = [1,2,3,a,5,6,7,8]

# -- deep copy

x = copy.deepcopy(b)

print('original lists')
print('deep b = {}'.format(b))
print('deep x = {}'.format(x))
print('delete b[0]')
del(b[0])

print('deep b = {}'.format(b))
print('deep x = {}'.format(x))

print('delete a[1]')
del(a[1])

print('deep b = {}'.format(b))
print('deep x = {}'.format(x))