eolas/zk/Sets_in_Python.md

104 lines
1.7 KiB
Markdown
Raw Normal View History

2023-02-14 09:16:11 +00:00
---
tags: [python, data-structures]
---
# Sets in Python
- They are **unordered**
- You can increase/decrease their length by adding/removing new members
- They **do not allow duplicate members**
- **Can only hold immutable objects**
2023-02-15 07:36:48 +00:00
> Sets are denoted with `{...}`
## Basic usage
```python
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket) # show that duplicates have been removed
print(len(basket))
# {'apple', 'pear', 'banana', 'orange'}
# 4
```
## Looping through sets
```python
for item in basket:
print(item)
"""
apple
pear
banana
orange
"""
```
## Check for membership
```python
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print('apple' in basket)
# True
```
## Remove items from set
> `remove` will raise an error if the specified item does not exist, `discard`
> will not
2023-02-15 07:36:48 +00:00
```python
basket.remove('apple')
basket.discard('apricot')
print(basket)
# {'pear', 'banana', 'orange'}
2023-02-15 15:31:31 +00:00
basket.clear()
print(basket)
#set
2023-02-15 07:36:48 +00:00
```
## Add items to a set
2023-02-15 15:31:31 +00:00
```python
basket.add('apricot')
print(basket)
# {'apricot', 'pear', 'banana', 'orange'}
```
2023-04-03 07:15:40 +01:00
## Start with empty set
To declare an empty set you cannot just do:
```py
my_set = {}
```
You have to use a constructor:
```py
my_set = set()
```
2023-02-15 15:31:31 +00:00
## Apply unions and intersections
```python
s1 = {'apple', 'orange', 'banana'}
s2 = {'grapefruit', 'lime', 'banana'}
print('Union:', s1 | s2)
# Union: {'apple', 'orange', 'grapefruit', 'lime', 'banana'}
print('Intersection:', s1 & s2)
# Intersection: {'banana'}
print('Difference:', s1 - s2)
# Difference: {'orange', 'apple'}
print('Symmetric Difference:', s1 ^ s2)
#Symmetric Difference: {'apple', 'orange', 'grapefruit', 'lime'}
```