Program for Set copy() Method

Python Program for Set copy() Method

Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The given set is :
{'this', 'hello', 'btechgeeks', 'is'}
The New set after copying all the values from the given set is:
{'this', 'hello', 'btechgeeks', 'is'}

Explanation:

Here all the values of the given set are copied into the new set using the copy() function.

Example2:

Input:

Given set = {'good', 'morning', 'btechgeeks'}

Output:

The given set is :
{'btechgeeks', 'morning', 'good'}
The New set after copying all the values from the given set is:
{'btechgeeks', 'morning', 'good'}

Explanation:

Here all the values of the given set are copied into the new set using the copy() function.

Program for Set copy() Method in Python

set copy() Method:

The Python set copy method is used to copy the entire set into a new set.

Syntax:

set.copy()

Parameters: The copy() method for sets does not accept any parameters.

Return value: The function returns a shallow copy of the original set as its return value.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply copy() function to the given set to copy all its elements into the new set.
  • Store it in another variable.
  • Print the above new set after copying all the values from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply copy() function to the given set to copy all its elements into the new set.
# Store it in another variable.
gvnset_copy = gven_set.copy()
# Print the above new set after copying all the values from the given set
print("The New set after copying all the values from the given set is:")
print(gvnset_copy)

Output:

The given set is :
{'this', 'hello', 'btechgeeks', 'is'}
The New set after copying all the values from the given set is:
{'this', 'hello', 'btechgeeks', 'is'}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply copy() function to the given set to copy all its elements into the new set.
  • Store it in another variable.
  • Print the above new set after copying all the values from the given set.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), input(), and split() functions.
# Store it in a variable.
gven_set = set(input("Enter some random values separated by spaces = ").split())
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply copy() function to the given set to copy all its elements into the new set.
# Store it in another variable.
gvnset_copy = gven_set.copy()
# Print the above new set after copying all the values from the given set
print("The New set after copying all the values from the given set is:")
print(gvnset_copy)

Output:

Enter some random values separated by spaces = good morning btechgeeks
The given set is :
{'morning', 'btechgeeks', 'good'}
The New set after copying all the values from the given set is:
{'morning', 'btechgeeks', 'good'}