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 merge_dictionaries(left: dict, right: dict) -> dict: | |
"""Merge two dictionaries using a shallow copy.""" | |
temp: dict = left.copy() | |
temp.update(right) | |
return temp | |
countries: dict = { | |
"Taiwan": 36193, | |
"Canada": 9984670, |
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 dict_from_two_lists(keys: list, values: list) -> dict: | |
""" | |
Args: | |
keys: The list of keys for the dictionary | |
values: The list of values for the dictionary | |
Returns: A dictionary of key:value pairs | |
""" |
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 | |
import requests | |
def get_values_by_key(dictionary: dict, key: str) -> dict: | |
"""get_values_by_key. | |
Args: | |
dictionary (dict): dictionary | |
key (str): key |
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 | |
import requests | |
def get_key_by_value(dictionary: dict, val: str) -> object: | |
"""get_values_by_key. | |
Args: | |
dictionary (dict): dictionary | |
val (str): 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
asci_uppercase: dict = {i: chr(+i) for i in range(65, 91, 1)} | |
digits: dict = {i: chr(+i) for i in range(48, 58, 1)} | |
asci_lowercase: dict = {i: chr(+i) for i in range(97, 123, 1)} | |
asci_punctuation: dict = {i: chr(+i) for i in range(32, 48, 1)} | |
asci_punctuation.update({i: chr(+i) for i in range(123, 127, 1)}) | |
asci_punctuation.update({i: chr(+i) for i in range(91, 97, 1)}) | |
asci_punctuation.update({i: chr(+i) for i in range(58, 65, 1)}) | |
asci_extended: dict = {i: chr(+i) for i in range(128, 255, 1)} | |
asci_system: dict = {i: hex(i) for i in range(0, 32, 1)} |
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) |
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 | |
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 | |
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
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}.") |