## Set Mutations - Python HackerRank Solution

Hello Friends, How are you? Today I am going to solve the

**HackerRank Set Mutations Problem in Python**with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-**{tocify} $title={Table of Contents}**

### HackerRank Python Set Mutations Solution - Problem Statement

We have seen the applications of union, intersection, difference and symmetric difference operations, but these operations do not make any changes or mutations to the set.

**We can use the following operations to create mutations to a set:**

**.update() or |=**

Update the set by adding elements from an iterable/another set.

>>> H = set("Hacker") >>> R = set("Rank") >>> H.update(R) >>> print H set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R']) {codeBox}

**.intersection_update() or &=**

Update the set by keeping only the elements found in it and an iterable/another set.

>>> H = set("Hacker") >>> R = set("Rank") >>> H.intersection_update(R) >>> print H set(['a', 'k']) {codeBox}

**.difference_update() or -=**

Update the set by removing elements found in an iterable/another set.

>>> H = set("Hacker") >>> R = set("Rank") >>> H.difference_update(R) >>> print H set(['c', 'e', 'H', 'r']) {codeBox}

**.symmetric_difference_update() or ^=**

Update the set by only keeping the elements found in either set, but not in both.

>>> H = set("Hacker") >>> R = set("Rank") >>> H.symmetric_difference_update(R) >>> print H set(['c', 'e', 'H', 'n', 'r', 'R']) {codeBox}

**Task**

You are given a set A and N number of other sets. These N number of sets have to perform some specific mutation operations on set A.

Your task is to execute those operations and print the sum of elements from set A.

**Input Format**

The first line contains the number of elements in set A.

The second line contains the space-separated list of elements in set A.

The third line contains integer N, the number of other sets.

The next 2* N lines are divided into N parts containing two lines each.

The first line of each part contains the space-separated entries of the operation name and the length of the other set.

The second line of each part contains a space-separated list of elements in the other set.

**Constraints**

0 < len(set(A)) < 1000

0 < len(otherSets) < 1000

0 < N < 100

**Output Format:**

Output the sum of elements in set A.

**Sample Input 0**

16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 24 52 4 intersection_update 10 2 3 5 6 8 9 1 4 7 11 update 2 55 66 symmetric_difference_update 5 22 7 35 62 58 difference_update 7 11 22 35 55 58 62 66 {codeBox}

**Sample Output 0**

38 {codeBox}

**Explanation**

After the first operation, (intersection_update operation), we get:

set A = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 11])

After the second operation, (update operation), we get:

set A = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 55, 66])

After the third operation, (symmetric_difference_update operation), we get:

set A = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 35, 55, 58, 62, 66])

After the fourth operation, ( difference_update operation), we get:

set A = set([1, 2, 3, 4, 5, 6, 7, 8, 9])

The sum of elements in set A after these operations is 38.

### Python Set Mutations - Hacker Rank Solution

**Approach I: Set Mutations HackerRank Python Solution**

```
# ========================
# Information
# ========================
# Name: Set Mutations in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/py-set-mutations/problem
# Difficulty: Easy
# Max Score: 10
# Language: Pypy 3
# ========================
# Solution Start
# ========================
#Set Mutations in Python - Hacker Rank Solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
len_set = int(input())
storage = set(map(int, input().split()))
op_len = int(input())
for i in range(op_len):
operation = input().split()
if operation[0] == 'intersection_update':
temp_storage = set(map(int, input().split()))
storage.intersection_update(temp_storage)
elif operation[0] == 'update':
temp_storage = set(map(int, input().split()))
storage.update(temp_storage)
elif operation[0] == 'symmetric_difference_update':
temp_storage = set(map(int, input().split()))
storage.symmetric_difference_update(temp_storage)
elif operation[0] == 'difference_update':
temp_storage = set(map(int, input().split()))
storage.difference_update(temp_storage)
else :
assert False
print(sum(storage))
#Set Mutations in Python - Hacker Rank Solution END
# MyEduWaves
```

**Disclaimer:**The above Problem (

**Python Set Mutations**) is generated by

**Hackerrank**but the Solution is Provided by

**MyEduWaves**. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit

**Python Set Mutations Hackerrank Problem**and try to solve it again.**All the Best!**

Tags:
HackerRank Python