What is a Hashtable/Hashmap?

A hashtable is a data structure that with a collection of key-value pairs, where each key maps to a value, and the keys must be unique and hashable.

  • In Python there is a built in hashtable known as a ___.

The primary purpose of a hashtable is to provide efficient lookup, insertion, and deletion operations. When an element is to be inserted into the hashtable, a hash function is used to map the key to a specific index in the underlying array that is used to store the key-value pairs. The value is then stored at that index. When searching for a value, the hash function is used again to find the index where the value is stored.

The key advantage of a hashtable over other data structures like arrays and linked lists is its average-case time complexity for lookup, insertion, and deletion operations.

  • The typical time complexity of a hashtable is ___.

What is Hashing and Collision?

Hashing is the process of mapping a given key to a value in a hash table or hashmap, using a hash function. The hash function takes the key as input and produces a hash value or hash code, which is then used to determine the index in the underlying array where the value is stored. The purpose of hashing is to provide a quick and efficient way to access data, by eliminating the need to search through an entire data structure to find a value.

However, it is possible for two different keys to map to the same hash value, resulting in a collision. When a collision occurs, there are different ways to resolve it, depending on the collision resolution strategy used.

Python's dictionary implementation is optimized to handle collisions efficiently, and the performance of the dictionary is generally very good, even in the presence of collisions. However, if the number of collisions is very high, the performance of the dictionary can degrade, so it is important to choose a good hash function that minimizes collisions when designing a Python dictionary.

What is a Set?

my_set = set([1, 2, 3, 2, 1])
print(my_set)  

# What do you notice in the output?
# It removes repeats
#

# Why do you think Sets are in the same tech talk as Hashmaps/Hashtables?
# We need to avoid repeating items in Hashmaps and Hashtables, so we may use similar concepts.
#

Dictionary Example

Below are just some basic features of a dictionary. As always, documentation is always the main source for all the full capablilties.

red_album = {
    "title": "Red (Taylor's Version)",
    "artist": "Taylor Swift",
    "year": 2021,
    "genre": ["Pop", "Synth-pop", "Country"],
    "tracks": {
        1: "State of Grace",
        2: "Red",
        3: "Treacherous",
        4: "I Knew You Were Trouble (Taylor's Version)",
        5: "All Too Well (Taylor's Version)",
        6: "22 (Taylor's Version)",
        7: "I Almost Do (Taylor's Version)",
        8: "We Are Never Ever Getting Back Together (Taylor's Version)",
        9: "Stay Stay Stay (Taylor's Version)",
        10: "The Last Time (Taylor's Version)",
        11: "Holy Ground (Taylor's Version)",
        12: "Sad Beautiful Tragic (Taylor's Version)",
        13: "The Lucky One (Taylor's Version)",
        14: "Everything Has Changed (Taylor's Version)",
        15: "Starlight (Taylor's Version)",
        16: "Begin Again (Taylor's Version)",
        17: "The Moment I Knew (Taylor's Version)",
        18: "Come Back...Be Here (Taylor's Version)",
        19: "Girl At Home (Taylor's Version)",
        20: "Ronan (Taylor's Version)",
        21: "Better Man (Taylor's Version)[From the Vault]",
        22: "Nothing New (Taylor's Version)[From the Vault]",
        23: "Babe (Taylor's Version)[From the Vault]",
        24: "Message In A Bottle (Taylor's Version)[From the Vault]",
        25: "I Bet You Think About Me (Taylor's Version)[From the Vault]",
        26: "Forever Winter (Taylor's Version)[From the Vault]",
        27: "Run (Taylor's Version)[From the Vault]",
        28: "The Very First Night (Taylor's Version)[From the Vault]",
        29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"
    
            }
    }


# What data structures do you see?
# Lists and dictionaries
#

# Printing the dictionary
print(red_album)
{'title': "Red (Taylor's Version)", 'artist': 'Taylor Swift', 'year': 2021, 'genre': ['Pop', 'Synth-pop', 'Country'], 'tracks': {1: 'State of Grace', 2: 'Red', 3: 'Treacherous', 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: "Girl At Home (Taylor's Version)", 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}}
print(red_album.get('tracks'))
# or
print(red_album['tracks'])
{1: "State of Grace (Taylor's Version)", 2: "Red (Taylor's Version)", 3: "Treacherous (Taylor's Version)", 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: "Girl At Home (Taylor's Version)", 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}
{1: "State of Grace (Taylor's Version)", 2: "Red (Taylor's Version)", 3: "Treacherous (Taylor's Version)", 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: "Girl At Home (Taylor's Version)", 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}
print(red_album.get('tracks')[4])
# or
print(red_album['tracks'][4])
I Knew You Were Trouble (Taylor's Version)
I Knew You Were Trouble (Taylor's Version)
red_album["producer"] = ['Taylor Swift', 'Jack Antonoff', 'Joel Little', 'Taylor Swift', 'Louis Bell', 'Frank Dukes']

# What can you change to make sure there are no duplicate producers?
#
# make it a set

red_album["producer"] = set(['Taylor Swift', 'Jack Antonoff', 'Joel Little', 'Taylor Swift', 'Louis Bell', 'Frank Dukes'])

# Printing the dictionary
print(red_album)
{'title': "Red (Taylor's Version)", 'artist': 'Taylor Swift', 'year': 2021, 'genre': ['Pop', 'Synth-pop', 'Country'], 'tracks': {1: "State of Grace (Taylor's Version)", 2: "Red (Taylor's Version)", 3: "Treacherous (Taylor's Version)", 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: 'All Too Well (10 minute version)', 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}, 'producer': {'Louis Bell', 'Joel Little', 'Frank Dukes', 'Taylor Swift', 'Jack Antonoff'}}
import json

# Adding a an key-value pair to an existing key 
red_album["tracks"].update({19: "All Too Well (10 minute version)"})

# How would add an additional genre to the dictionary, like electropop? 
# 
#red_album["genre"].update({20: "Pop-rock"})

# Printing the dictionary
print(red_album)
{'title': "Red (Taylor's Version)", 'artist': 'Taylor Swift', 'year': 2021, 'genre': ['Pop', 'Synth-pop', 'Country'], 'tracks': {1: "State of Grace (Taylor's Version)", 2: "Red (Taylor's Version)", 3: "Treacherous (Taylor's Version)", 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: 'All Too Well (10 minute version)', 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}, 'producer': {'Louis Bell', 'Joel Little', 'Frank Dukes', 'Taylor Swift', 'Jack Antonoff'}}
for k,v in red_album.items(): # iterate using a for loop for key and value
    print(str(k) + ": " + str(v))

# Write your own code to print tracks in readable format
print(json.dumps(red_album['tracks'], indent=4))
title: Red (Taylor's Version)
artist: Taylor Swift
year: 2021
genre: ['Pop', 'Synth-pop', 'Country']
tracks: {1: "State of Grace (Taylor's Version)", 2: "Red (Taylor's Version)", 3: "Treacherous (Taylor's Version)", 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: 'All Too Well (10 minute version)', 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}
producer: {'Louis Bell', 'Joel Little', 'Frank Dukes', 'Taylor Swift', 'Jack Antonoff'}
{
    "1": "State of Grace (Taylor's Version)",
    "2": "Red (Taylor's Version)",
    "3": "Treacherous (Taylor's Version)",
    "4": "I Knew You Were Trouble (Taylor's Version)",
    "5": "All Too Well (Taylor's Version)",
    "6": "22 (Taylor's Version)",
    "7": "I Almost Do (Taylor's Version)",
    "8": "We Are Never Ever Getting Back Together (Taylor's Version)",
    "9": "Stay Stay Stay (Taylor's Version)",
    "10": "The Last Time (Taylor's Version)",
    "11": "Holy Ground (Taylor's Version)",
    "12": "Sad Beautiful Tragic (Taylor's Version)",
    "13": "The Lucky One (Taylor's Version)",
    "14": "Everything Has Changed (Taylor's Version)",
    "15": "Starlight (Taylor's Version)",
    "16": "Begin Again (Taylor's Version)",
    "17": "The Moment I Knew (Taylor's Version)",
    "18": "Come Back...Be Here (Taylor's Version)",
    "19": "All Too Well (10 minute version)",
    "20": "Ronan (Taylor's Version)",
    "21": "Better Man (Taylor's Version)[From the Vault]",
    "22": "Nothing New (Taylor's Version)[From the Vault]",
    "23": "Babe (Taylor's Version)[From the Vault]",
    "24": "Message In A Bottle (Taylor's Version)[From the Vault]",
    "25": "I Bet You Think About Me (Taylor's Version)[From the Vault]",
    "26": "Forever Winter (Taylor's Version)[From the Vault]",
    "27": "Run (Taylor's Version)[From the Vault]",
    "28": "The Very First Night (Taylor's Version)[From the Vault]",
    "29": "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"
}
def search():
    search = input("What would you like to know about the album?")
    if red_album.get(search.lower()) == None:
        print("Invalid Search")
    else:
        print(red_album.get(search.lower()))

search()
{1: "State of Grace (Taylor's Version)", 2: "Red (Taylor's Version)", 3: "Treacherous (Taylor's Version)", 4: "I Knew You Were Trouble (Taylor's Version)", 5: "All Too Well (Taylor's Version)", 6: "22 (Taylor's Version)", 7: "I Almost Do (Taylor's Version)", 8: "We Are Never Ever Getting Back Together (Taylor's Version)", 9: "Stay Stay Stay (Taylor's Version)", 10: "The Last Time (Taylor's Version)", 11: "Holy Ground (Taylor's Version)", 12: "Sad Beautiful Tragic (Taylor's Version)", 13: "The Lucky One (Taylor's Version)", 14: "Everything Has Changed (Taylor's Version)", 15: "Starlight (Taylor's Version)", 16: "Begin Again (Taylor's Version)", 17: "The Moment I Knew (Taylor's Version)", 18: "Come Back...Be Here (Taylor's Version)", 19: 'All Too Well (10 minute version)', 20: "Ronan (Taylor's Version)", 21: "Better Man (Taylor's Version)[From the Vault]", 22: "Nothing New (Taylor's Version)[From the Vault]", 23: "Babe (Taylor's Version)[From the Vault]", 24: "Message In A Bottle (Taylor's Version)[From the Vault]", 25: "I Bet You Think About Me (Taylor's Version)[From the Vault]", 26: "Forever Winter (Taylor's Version)[From the Vault]", 27: "Run (Taylor's Version)[From the Vault]", 28: "The Very First Night (Taylor's Version)[From the Vault]", 29: "All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]"}
search_string = "(Taylor's Version)"
track_dict = red_album['tracks']

for key, value in track_dict.items():
    if search_string in value:
        print(f"{key}: {value}")
4: I Knew You Were Trouble (Taylor's Version)
5: All Too Well (Taylor's Version)
6: 22 (Taylor's Version)
7: I Almost Do (Taylor's Version)
8: We Are Never Ever Getting Back Together (Taylor's Version)
9: Stay Stay Stay (Taylor's Version)
10: The Last Time (Taylor's Version)
11: Holy Ground (Taylor's Version)
12: Sad Beautiful Tragic (Taylor's Version)
13: The Lucky One (Taylor's Version)
14: Everything Has Changed (Taylor's Version)
15: Starlight (Taylor's Version)
16: Begin Again (Taylor's Version)
17: The Moment I Knew (Taylor's Version)
18: Come Back...Be Here (Taylor's Version)
19: Girl At Home (Taylor's Version)
20: Ronan (Taylor's Version)
21: Better Man (Taylor's Version)[From the Vault]
22: Nothing New (Taylor's Version)[From the Vault]
23: Babe (Taylor's Version)[From the Vault]
24: Message In A Bottle (Taylor's Version)[From the Vault]
25: I Bet You Think About Me (Taylor's Version)[From the Vault]
26: Forever Winter (Taylor's Version)[From the Vault]
27: Run (Taylor's Version)[From the Vault]
28: The Very First Night (Taylor's Version)[From the Vault]
29: All Too Well (Ten Minute Version) (Taylor's Version)[From the Vault]

Hacks

  • Answer ALL questions in the code segments
  • Create a diagram or comparison illustration (Canva).
    • What are the pro and cons of using this data structure?
    • Dictionary vs List
  • Expand upon the code given to you, possible improvements in comments
  • Build your own album showing features of a python dictionary

  • For Mr. Yeung's class: Justify your favorite Taylor Swift song, answer may effect seed