9 of 151 menu

The copy method

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

Syntax

list.copy()

Example

Let's copy our list using the copy method:

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

Result of code execution:

['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:

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

Result of code execution:

['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
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline