# Sets

{% embed url="<https://www.hackerrank.com/domains/python?badge_type=python&filters%5Bsubdomains%5D%5B%5D=py-sets>" %}

## Introduction to Sets

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2F2xddyfYPQWUH515QbPQy%2Fimage.png?alt=media&#x26;token=36383db7-3bb8-4354-8141-66e4114496ba" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
def average(array):
    distinct = set(array)
    return sum(distinct) / len(distinct)

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    result = average(arr)
    print(result)
```

{% endcode %}

***

## Set .add()

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2Fscal4EQWlKAcIVWI6xca%2Fimage.png?alt=media&#x26;token=2fe52d20-6a39-4c0f-a7e7-5f4efcd795e4" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
distinct = set()

for _ in range(int(input())):
    distinct.add(input())
    
print(len(distinct))
```

{% endcode %}

***

## Set .discard(), .remove(), .pop()

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2Fxo1nvkfQOnutYZhK8yvg%2Fimage.png?alt=media&#x26;token=9a71f075-552b-4d56-8d79-9a56ae64ba5d" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
n = int(input())
s = set(map(int, input().split()))

for _ in range(int(input())):
    command = input().split()
    
    if command[0] == "discard":
        s.discard(int(command[1]))
    elif command[0] == "remove":
        s.remove(int(command[1]))
    elif command[0] == "pop":
        s.pop()
        
print(sum(s))
```

{% endcode %}

***

## Set .union() Operation

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2F2LygfVYrULmWlScMAcuT%2Fimage.png?alt=media&#x26;token=b1f85955-dbfe-4a3d-9839-8f3c4e5af7d2" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.union(french_newspaper)))
```

{% endcode %}

***

## Set .intersection() Operation

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FegPOIgNtroCxkYKbUa3Q%2Fimage.png?alt=media&#x26;token=4ead673a-3a43-4924-a99f-73b76f47a5c3" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.intersection(french_newspaper)))
```

{% endcode %}

***

## Set .difference() Operation

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2Fb6yHIqXjmr2VYpISxVYJ%2Fimage.png?alt=media&#x26;token=8ca8b1fb-460d-4bf4-9e6a-60dd6d45b1fd" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.difference(french_newspaper)))
```

{% endcode %}

***

## Set .symmetric\_difference() Operation

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FLiiixEj3Glj9lhHKfsnD%2Fimage.png?alt=media&#x26;token=f5cc0b72-43bd-4dd6-a009-112235f1bd62" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
n = int(input())
english_newspaper = set(map(int, input().split()))
m = int(input())
french_newspaper = set(map(int, input().split()))

print(len(english_newspaper.symmetric_difference(french_newspaper)))
```

{% endcode %}

***

## Symmetric Difference

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2F7NWfgeX1v5rLbH2A7V9L%2Fimage.png?alt=media&#x26;token=002c1e88-6b85-4b2b-a10c-cc0f6c236550" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
m = int(input())
M = set(map(int, input().split()))
n = int(input())
N = set(map(int, input().split()))

for each in sorted(M.symmetric_difference(N)):
    print(each)
```

{% endcode %}

***

## Set Mutations

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FNhPknUu6XnJnlCXbTVb8%2Fimage.png?alt=media&#x26;token=98d7976b-0ab2-49e2-973d-0396f5566bfe" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
n = int(input())
set_a = set(map(int, input().strip().split()))

for _ in range(int(input())):
    command = input().strip().split()
    set_b = set(map(int, input().strip().split()))
    if command[0] == "intersection_update":
        set_a.intersection_update(set_b)
    if command[0] == "symmetric_difference_update":
        set_a.symmetric_difference_update(set_b)
    if command[0] == "difference_update":
        set_a.difference_update(set_b)
    if command[0] == "update":
        set_a.update(set_b)
        
print(sum(set_a))
```

{% endcode %}

***

## The Captain's Room

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2Fr6TtSk7VoxWYvGyu1o54%2Fimage.png?alt=media&#x26;token=1fbd340e-6e7c-4ae2-82d2-2aff34a9937f" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
K = int(input())
room_numbers = list(map(int, input().split()))
distinct_room_numbers = set(room_numbers)

for each in distinct_room_numbers:
    room_numbers.remove(each)

print(distinct_room_numbers.difference(set(room_numbers)).pop())
```

{% endcode %}

***

## Check Subset

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FTtHcup7xG8qECDKJSza3%2Fimage.png?alt=media&#x26;token=40c6dec2-e6fd-4350-b21a-8177e483ed6e" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
for _ in range(int(input())):
    a = int(input())
    A = set(map(int, input().split()))
    b = int(input())
    B = set(map(int, input().split()))
    
    if A.issubset(B):
        print(True)
    else:
        print(False)
```

{% endcode %}

***

## Check Strict Superset

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FkTB3KqOWs8TRC0amBzGv%2Fimage.png?alt=media&#x26;token=4c3b9419-627a-475a-abc2-245b14d9c9e1" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
A = set(map(int, input().split()))

for _ in range(int(input())):
    if A.issuperset(set(map(int, input().split()))):
        pass
    else:
        print(False)
        exit()
    
print(True)
```

{% endcode %}

***

## No Idea!

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FPIvUhFMMAWbs41hG37pW%2Fimage.png?alt=media&#x26;token=231dbd81-b4e7-443b-8475-545513e77c96" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
happiness = 0

n, m = map(int, input().split())
arr = list(map(int, input().split()))
A = set(map(int, input().split()))
B = set(map(int, input().split()))

for each in arr:
    if each in A:
        happiness += 1
    if each in B:
        happiness -= 1
        
print(happiness)
```

{% endcode %}
