# Set .discard(), .remove() & .pop() HackerRank Solution in Python

### Problem

#### .remove(x)

This operation removes element x from the set.

If element x does not exist, it raises a KeyError.

The .remove(x) operation returns None.

### Example

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

>>> s.remove(5)

>>> print s

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

>>> print s.remove(4)

None

>>> print s

set([1, 2, 3, 6, 7, 8, 9])

>>> s.remove(0)

KeyError: 0
```

This operation also removes element x from the set.

If element x does not exist, it does not raise a KeyError.

### Example

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

>>> print s

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

None

>>> print s

set([1, 2, 3, 6, 7, 8, 9])

>>> print s

set([1, 2, 3, 6, 7, 8, 9])
```

#### .pop()

This operation removes and returns an arbitrary element from the set.

If there are no elements to remove, it raises a KeyError.

### Example

```>>> s = set([1])

>>> print s.pop()

1

>>> print s

set([])

>>> print s.pop()

KeyError: pop from an empty set
```

You have a non-empty set of s, and you have to execute N commands given in N lines.

The commands will be pop, remove and discard.

### Input Format

The first line contains integer n, the number of elements in the set s.

The second line contains n space-separated elements of set s. All of the elements are non-negative integers, less than or equal to 9.

The third line contains integer N, the number of commands.

The next N lines contain either pop, remove and/or discard commands followed by their associated value.

### Output Format

Print the sum of the elements of set s on a single line.

### Sample Input

9

1 2 3 4 5 6 7 8 9

10

pop

remove 9

remove 7

pop

remove 5

pop

4

### Explanation

After completing these 10 operations on the set, we get set. Hence, the sum is 4.

Note: Convert the elements of set s to integers while you are assigning them. To ensure the proper input of the set, we have added the first two lines of code to the editor.

### Solution in Python

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14``` ```num = int(input()) data = set(map(int, input().split())) operations = int(input()) for x in range(operations): oper = input().split() if oper[0] == "remove": data.remove(int(oper[1])) elif oper[0] == "discard": data.discard(int(oper[1])) else: data.pop() print(sum(data)) ```

No Comment