Python Set copy method with Example – Python Tutorial

1 year ago Lalit Bhagtani 0

In this tutorial, we will learn about python set copy method.
Python Set Copy

copy method :

This method returns a shallow copy of another set. It create a new set, which contains all the element of a set on which copy() method is called and returned it.

Syntax : < Set Object >.copy( ) 

Example:

[datacamp_exercise lang=”python” height=”300″ min-height=”200″ max-height=”500″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# create set object and assign it to variable s
s = {1,2,3,4,5}

# print all elements of set s
print(s)

# create a copy of set s and assign it to variable s1
s1 = s.copy()

# print all elements of set s1
print(s1)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

References :-

  1. copy method Docs

That’s all for Python Set copy method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous