From 11bbfdde4d6eb1b89feb0a9c14970df0044cc586 Mon Sep 17 00:00:00 2001 From: Nick Korostelev Date: Sun, 22 Sep 2024 20:54:25 -0700 Subject: [PATCH 1/3] Python: add Set and modify Dict --- python.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python.md b/python.md index 46a4236380..9d21c0d8b1 100644 --- a/python.md +++ b/python.md @@ -46,6 +46,9 @@ 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... @@ -55,6 +58,15 @@ dict.setdefault("key", 1) **dict # expands all k/v pairs in place ``` +### Set + +```py +dict = {} +set = {1, 2, 3} +set.add(4) +set.remove(2) +``` + ### Iteration ```py From e78141763c0a4c1cb443ee241068a3b66131d74c Mon Sep 17 00:00:00 2001 From: Nick Korostelev Date: Sun, 22 Sep 2024 21:02:29 -0700 Subject: [PATCH 2/3] Apply suggestions from code review --- python.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.md b/python.md index 9d21c0d8b1..fb1a1f74b6 100644 --- a/python.md +++ b/python.md @@ -61,7 +61,7 @@ dict.setdefault("key", 1) ### Set ```py -dict = {} +set = set() set = {1, 2, 3} set.add(4) set.remove(2) From 23230602e89c9890066d4b4c974c998a4870a647 Mon Sep 17 00:00:00 2001 From: Nick Korostelev Date: Sun, 22 Sep 2024 21:10:46 -0700 Subject: [PATCH 3/3] Update python.md --- python.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python.md b/python.md index fb1a1f74b6..9211ebc758 100644 --- a/python.md +++ b/python.md @@ -52,8 +52,9 @@ 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 ```