This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def remove_even_items(the_dict: dict): | |
# collect the keys of all the items to remove | |
delete_these = set(k for k, v in the_dict.items() if v % 2 == 0) | |
for delete_this in delete_these: | |
del the_dict[delete_this] | |
return the_dict |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from datetime import datetime | |
class PythonObjectEncoder(json.JSONEncoder): | |
def default(self, an_object_value): | |
if isinstance(an_object_value, str): | |
return an_object_value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import json | |
class PythonObjectEncoder(json.JSONEncoder): | |
"""PythonObjectEncoder. | |
""" | |
def default(self, an_object_value): | |
"""default. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def invert_dictionary(input:dict)->dict: | |
return {v: k for k, v in dict1.items()} | |
dict1 = {"a": 1, "b": 2, "c": 3} | |
assert invert_dictionary(dict1) == {1: "a", 2: "b", 3: "c"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
original = {} | |
original["object1"] = dict({"a": {"b": [1, 2, 3]}}) | |
dict_shallow = original.copy() | |
dict_deep = copy.deepcopy(original) | |
# change the mutable object in original and dict_shallow | |
original["object1"]["a"]["b"] = [3, 4, 5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
digits: dict = {i: chr(+i) for i in range(48, 58, 1)} | |
key = 50 | |
try: | |
val = digits.pop(key) | |
except KeyError: | |
print (f"The item with key {key} did not exist.") | |
else: | |
print(f"Deleted item with key {key} with value {val}.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
import csv | |
import requests | |
url: str = "https://data.london.gov.uk/download/london-borough-profiles/c1693b82-68b1-44ee-beb2-3decf17dc1f8/london-borough-profiles.csv " | |
boroughs = (requests.get(url).text).split("\n") | |
reader = csv.DictReader(boroughs, dialect="excel") | |
dict1 = dd(dict) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
import requests | |
import csv | |
from pprint import pprint | |
url: str = "https://data.london.gov.uk/download/london-borough-profiles/c1693b82-68b1-44ee-beb2-3decf17dc1f8/london" "-borough-profiles.csv " | |
boroughs = (requests.get(url).text).split("\n") | |
reader = csv.DictReader(boroughs, dialect="excel") | |
dict1 = dd(dict) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
s = [("John", "Male", 25), ("Fred", "Female", 48), ("Sam", "Female", 41), | |
("Jane", "Female", 25)] | |
dict1 = dd(dict) | |
for name, gender, age in s: | |
dict1[name]["age"] = age | |
dict1[name]["gender"] = gender |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict as dd | |
s = [("John", "Male"), ("John", "48"), ("John", "Married"), ("Jane", "Female"), | |
("Jane", "25")] | |
dict1: dict = dd(list) | |
for k, v in s: | |
dict1[k].append(v) |