Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

list.copy

Creates a shallow copy of the list.

Version: Python 3.14; repository image, production runtime not attested

Syntax

a.copy()

Creates a shallow copy of the list.

The outer list is distinct, but nested mutable elements are still shared.

The new outer list receives references to the same elements. Adding 4 to the nested list through the copy changes that shared element inside the original too. The identity comparison of the two outer lists remains false.

a = [[]]
b = a.copy()
assert a is not b and a[0] is b[0]

Examples

a = [[3]]
b = a.copy()
b[0].append(4)
print(a)
print(a is b)

Result

[[3, 4]]
False

The new outer list receives references to the same elements. Adding 4 to the nested list through the copy changes that shared element inside the original too. The identity comparison of the two outer lists remains false.

Sources