.copy()

NBarnhouse's avatar
Published Jun 28, 2024
Contribute to Docs

In Python, the .copy() method returns a shallow copy of a specified set. The shallow copy is a new set that contains references to the elements in the original set, not the elements themselves. It can be used when it is important to work with the new set without modifying the original set.

Syntax

set.copy()
  • set: The set to be copied.

Example

The following example demonstrates the usage of the .copy() method:

original_set = { 'First', 'Second', 'Third' }
print (original_set)
new_set = original_set.copy()
new_set.add('Fourth')
print(new_set)

The above code produces the following output:

{'First', 'Second', 'Third'}
{'First', 'Second', 'Third', 'Fourth'}

Note: Unlike lists, a set element has no order or index. Hence, though the elements in the output are shown in order, they can be placed differently in other cases.

Codebyte Example

The following codebyte example demonstrates the usage of the .copy() method:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy