The copy method

The copy method makes a copy of the list. We do not specify anything in the method parameter.

Syntax

pythonlist.copy()

Example

Let's copy our list using the copy method:

pythonlst1 = ['ab', 'cd', 'ef']
lst2 = lst1.copy()
print(lst2)

Result of code execution:

python['ab', 'cd', 'ef']

Example

However, by using the copy method we only create a so-called shallow copy of the list - changes we make to the original list after copying will not affect the copy:

pythonlst1 = ['ab', 'cd', 'ef']
lst2 = lst1.copy()
lst1.append('jh')

print(lst1)
print(lst2)

Result of code execution:

python['ab', 'cd', 'ef', 'jh']
['ab', 'cd', 'ef']

See also

  • method extend,
    which adds elements from the specified object to the list
  • method clear,
    which removes all elements of a list