|
| 1 | +######### INTRO TO DICTIONARIES ######### |
| 2 | + |
| 3 | +#dictionary stores key-value pairs |
| 4 | +#The equiv of an associative array/hash table |
| 5 | +emails = { |
| 6 | + |
| 7 | + |
| 8 | +} |
| 9 | + |
| 10 | +print(emails) |
| 11 | +#in this case, the key is the name, email is the value. |
| 12 | +#Data type doesn't matter at all for the value. |
| 13 | +#Key must be a hashable --> What does this mean? |
| 14 | +#Classes have a function __hash__ invoked when used as the key |
| 15 | +print(hash("hello")) |
| 16 | + |
| 17 | +#Almost always immutable type |
| 18 | +#a tuple will work, list will not. a number will work |
| 19 | + |
| 20 | +#Why use a hashtable? Extremely fast to add or look up data |
| 21 | +#O(1) --> constant time. More elements does not mean slower unlike a sort or something |
| 22 | +#https://en.wikipedia.org/wiki/Hash_table |
| 23 | +#Hashtables often used for memoization |
| 24 | + |
| 25 | +#Next up we gonna talk about retrieving data from a dictionary |
| 26 | + |
| 27 | + |
| 28 | +######### RETRIEVE DATA FROM DICTIONARY ########## |
| 29 | +print(list(emails)) |
| 30 | +print(sorted(emails)) |
| 31 | +#print(emails[0]) # NOPE! |
| 32 | +print(emails["Caleb"]) |
| 33 | + |
| 34 | +#Since data is not nicely sequenced |
| 35 | +#we may want to check before we try grabbing stuff |
| 36 | + |
| 37 | +if("Caleb" in emails): |
| 38 | + print("Emailing", emails["Caleb"]) |
| 39 | + |
| 40 | +#This may seem bad because we first check to see if its in |
| 41 | +#Then we do another line to get the value |
| 42 | +#but this is different than a list where we iterate to find the element |
| 43 | +#The key is goes through hash to calculate index. Either there or not |
| 44 | +#This is O(1) |
| 45 | + |
| 46 | +#You may still not like the casing and in that situation there is a method |
| 47 | + |
| 48 | +print(emails.get("Ryan")) #Returns none if not found |
| 49 | +print(emails.get("Ryan", "Not found")) #optional return arg if not found |
| 50 | + |
| 51 | + |
| 52 | +######### ADD DATA TO DICTIONARY ######### |
| 53 | + |
| 54 | +#How to add data (3 ways here): |
| 55 | +#indexing |
| 56 | +emails[ "josh"] = "[email protected]" |
| 57 | +print(emails) |
| 58 | + |
| 59 | +#update function |
| 60 | +emails. update( { "josh": "[email protected]"}) |
| 61 | +print(emails) |
| 62 | + |
| 63 | +#Weird variation |
| 64 | +emails. update( josh = "[email protected]") |
| 65 | +print(emails) |
| 66 | + |
| 67 | +#Key must be immutable... |
| 68 | +emails[5] = "test" |
| 69 | +emails[(1, 2)] = "yep" |
| 70 | +#emails[[5, 3]] = "nope" |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +######### LOOPING THROUGH KEYS ######### |
| 75 | +######### LOOPING THROUGH KEY-VALUE PAIRS ######### |
| 76 | +######### SETS EXPLAINED ######### |
| 77 | +######### REMOVE DUPLICATES FROM LIST / CREATE SET FROM LIST ########## |
| 78 | +######### UNION AND INTERSECTION ######### |
| 79 | +######### DIFFERENCE AND SYMMETRIC DIFFERENCE ######### |
0 commit comments