Skip to content

Commit

Permalink
Python: add Set and modify Dict (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkoroste authored Sep 24, 2024
1 parent 746bae2 commit 1fa8ae1
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions python.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,28 @@ sorted(list) # returns sorted copy of list

```py
dict = {}
dict = {'a': 1, 'b': 2}
dict['c'] = 3
del dict['a'] # Remove key-value pair with key 'c'
dict.keys()
dict.values()
"key" in dict # let's say this returns False, then...
dict["key"] # ...this raises KeyError
dict.get("key") # ...this returns None
dict["key"] # if "key" not in dict raises KeyError
dict.get("key") # if "key" not in dict returns None
dict.get("key", "optional return value if no key")
dict.setdefault("key", 1)
**dict # expands all k/v pairs in place
```

### Set

```py
set = set()
set = {1, 2, 3}
set.add(4)
set.remove(2)
```

### Iteration

```py
Expand Down

0 comments on commit 1fa8ae1

Please sign in to comment.