Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / dict_03_merge.py
Created July 30, 2020 16:20
Merge two dictionaries.
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,
@raeq
raeq / dict_04_twolists.py
Created August 1, 2020 18:25
Return a dict from two lists
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
"""
@raeq
raeq / dict_05_bykey.py
Created August 2, 2020 12:13
Find all nested keys in a dict
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
@raeq
raeq / dict_06_byvalue.py
Last active August 2, 2020 13:54
Get index pertaining to a value in nested dictionary
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
@raeq
raeq / dict_07_comp.py
Created August 2, 2020 20:16
Dictionary Comprehension
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)}
@raeq
raeq / dict_08_default.py
Created August 3, 2020 09:44
Using a defaultdict
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)
@raeq
raeq / dict_09_tuples.py
Created August 3, 2020 09:54
Tuples to Dictionary
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
@raeq
raeq / dict_10_fromcsv.py
Created August 3, 2020 12:32
Use the CSV package
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)
@raeq
raeq / dict_10_fromcsv.py
Created August 3, 2020 12:34
Use the CSV module
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)
@raeq
raeq / dict_11_removeitem.py
Created August 3, 2020 13:07
Delete an item.
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}.")