Python copy() Function

The copy() function in Python, is used to copy a list, dictionary, or a set. For example:

a = [9, 8, 10, 12]
print("The list \"a\" is:")

b = a.copy()
print("\nThe list \"b\" is:")

c = []
print("\nThe list \"c\" is:")

c = b.copy()
print("\nNow the list \"c\" is:")

The output produced by above Python program, demonstrating the copy() function, is:

The list "a" is:
[9, 8, 10, 12]

The list "b" is:
[9, 8, 10, 12]

The list "c" is:

Now the list "c" is:
[9, 8, 10, 12]

Here is another example, uses copy() function to copy a dictionary and a list in Python:

dictionaryOne = {"Name": "Brian", "State": "California"}
dictionaryTwo = dictionaryOne.copy()

setOne = {43, 54, 65}
setTwo = setOne.copy()

The output will exactly be:

{'Name': 'Brian', 'State': 'California'}
{65, 43, 54}

Python copy() Function Syntax

The syntax to use copy() function in Python, is:

varTwo = varOne.copy()

where varOne and varTwo are two variables of list, dict, or of set types.

The varOne gets copied to varTwo. Or all the items of varOne gets initialized to varTwo.

Python Online Test

« Previous Function Next Function »

Like/Share Us on Facebook 😋