Hacks

  • Answer ALL questions in the code segments
  • Create a venn diagram or other compare and contrast tool related to hashmaps.

    • What are the pro and cons of using this data structure?
    • Dictionary vs List
      Q16
  • Expand upon the code given to you, possible improvements in comments

  • Build your own album showing features of a python dictionary
rhcp_albums = { 
    'californication': {
        "title": "Californication",
        "artist": "Red Hot Chilli Peppers",
        "year": 1999,
        "genre": ["Rock", "Punk-rock", "Alternative-rock", "Funk Rock"],
        "tracks": {
            1: "Scar Tissue",
            2: "Savior",
            3: "Otherside",
            4: "Parallel Universe",
            5: "The Velvet Glove",
            6: "I Like Dirt",
            7: "Easily",
            8: "Emit Remmus",
            9: "Road Trippin'",
            10: "Get on Top",
            11: "Californication",
            12: "Porcelain",
            13: "Right On Time",
            14: "Around the World",
            15: "Purple Stain"
        }
    },
    'blood sugar sex magik': {
        "title": "Blood Sugar Sex Magik",
        "artist": "Red Hot Chili Peppers",
        "year": 1991,
        "genre": ["Rock", "Funk", "Alternative-rock"],
        "tracks": {
            1: "The Power of Equality",
            2: "If You Have to Ask",
            3: "Breaking the Girl",
            4: "Funky Monks",
            5: "Suck My Kiss",
            6: "I Could Have Lied",
            7: "Mellowship Slinky in B Major",
            8: "The Righteous & The Wicked",
            9: "Give It Away",
            10: "Blood Sugar Sex Magik",
            11: "Under the Bridge",
            12: "Naked in the Rain",
            13: "Apache Rose Peacock",
            14: "The Greeting Song",
            15: "My Lovely Man",
            16: "Sir Psycho Sexy",
            17: "They're Red Hot"
    }
    }
}

print(rhcp_albums.get('californication'))
print(rhcp_albums.get('Blood Sugar Sex Magik'))
{'title': 'Californication', 'artist': 'Red Hot Chilli Peppers', 'year': 1999, 'genre': ['Rock', 'Punk-rock', 'Alternative-rock', 'Funk Rock'], 'tracks': {1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain'}}
None
print(rhcp_albums.get('californication')['tracks'])
# or
print(rhcp_albums['californication']['tracks'])
{1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain'}
{1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain'}
print(rhcp_albums.get('californication')['tracks'][7])
# or
print(rhcp_albums['californication']['tracks'][1])
Easily
Scar Tissue
rhcp_albums["members"] = ['Anothony Kiedis', 'Flea', 'John Frusciante', 'Flea' 'Chad Smith', 'Dave Navarro']
# What can you change to make sure there are no duplicate producers?
# We can add a set or make this list into a set so it won't duplicate
#

# Printing the dictionary
print(rhcp_albums)
{'californication': {'title': 'Californication', 'artist': 'Red Hot Chilli Peppers', 'year': 1999, 'genre': ['Rock', 'Punk-rock', 'Alternative-rock', 'Funk Rock'], 'tracks': {1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain'}}, 'Blood Sugar Sex Magik': {'title': 'Blood Sugar Sex Magik', 'artist': 'Red Hot Chili Peppers', 'year': 1991, 'genre': ['Rock', 'Funk', 'Alternative-rock'], 'tracks': {1: 'The Power of Equality', 2: 'If You Have to Ask', 3: 'Breaking the Girl', 4: 'Funky Monks', 5: 'Suck My Kiss', 6: 'I Could Have Lied', 7: 'Mellowship Slinky in B Major', 8: 'The Righteous & The Wicked', 9: 'Give It Away', 10: 'Blood Sugar Sex Magik', 11: 'Under the Bridge', 12: 'Naked in the Rain', 13: 'Apache Rose Peacock', 14: 'The Greeting Song', 15: 'My Lovely Man', 16: 'Sir Psycho Sexy', 17: "They're Red Hot"}}, 'members': ['Anothony Kiedis', 'Flea', 'John Frusciante', 'FleaChad Smith', 'Dave Navarro']}
rhcp_albums["members"] = set(['Anothony Kiedis', 'Flea', 'John Frusciante', 'Flea', 'Chad Smith', 'Dave Navarro'])

print(rhcp_albums)
{'californication': {'title': 'Californication', 'artist': 'Red Hot Chilli Peppers', 'year': 1999, 'genre': ['Rock', 'Punk-rock', 'Alternative-rock', 'Funk Rock'], 'tracks': {1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain'}}, 'Blood Sugar Sex Magik': {'title': 'Blood Sugar Sex Magik', 'artist': 'Red Hot Chili Peppers', 'year': 1991, 'genre': ['Rock', 'Funk', 'Alternative-rock'], 'tracks': {1: 'The Power of Equality', 2: 'If You Have to Ask', 3: 'Breaking the Girl', 4: 'Funky Monks', 5: 'Suck My Kiss', 6: 'I Could Have Lied', 7: 'Mellowship Slinky in B Major', 8: 'The Righteous & The Wicked', 9: 'Give It Away', 10: 'Blood Sugar Sex Magik', 11: 'Under the Bridge', 12: 'Naked in the Rain', 13: 'Apache Rose Peacock', 14: 'The Greeting Song', 15: 'My Lovely Man', 16: 'Sir Psycho Sexy', 17: "They're Red Hot"}}, 'members': {'Flea', 'Chad Smith', 'John Frusciante', 'Anothony Kiedis', 'Dave Navarro'}}
rhcp_albums['californication']["tracks"].update({16: "New Track"})


print(rhcp_albums)
{'californication': {'title': 'Californication', 'artist': 'Red Hot Chilli Peppers', 'year': 1999, 'genre': ['Rock', 'Punk-rock', 'Alternative-rock', 'Funk Rock'], 'tracks': {1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain', 16: 'New Track'}}, 'Blood Sugar Sex Magik': {'title': 'Blood Sugar Sex Magik', 'artist': 'Red Hot Chili Peppers', 'year': 1991, 'genre': ['Rock', 'Funk', 'Alternative-rock'], 'tracks': {1: 'The Power of Equality', 2: 'If You Have to Ask', 3: 'Breaking the Girl', 4: 'Funky Monks', 5: 'Suck My Kiss', 6: 'I Could Have Lied', 7: 'Mellowship Slinky in B Major', 8: 'The Righteous & The Wicked', 9: 'Give It Away', 10: 'Blood Sugar Sex Magik', 11: 'Under the Bridge', 12: 'Naked in the Rain', 13: 'Apache Rose Peacock', 14: 'The Greeting Song', 15: 'My Lovely Man', 16: 'Sir Psycho Sexy', 17: "They're Red Hot"}}}
for k,v in rhcp_albums.items(): # iterate using a for loop for key and value
    print(str(k) + ": " + str(v))
californication: {'title': 'Californication', 'artist': 'Red Hot Chilli Peppers', 'year': 1999, 'genre': ['Rock', 'Punk-rock', 'Alternative-rock', 'Funk Rock'], 'tracks': {1: 'Scar Tissue', 2: 'Savior', 3: 'Otherside', 4: 'Parallel Universe', 5: 'The Velvet Glove', 6: 'I Like Dirt', 7: 'Easily', 8: 'Emit Remmus', 9: "Road Trippin'", 10: 'Get on Top', 11: 'Californication', 12: 'Porcelain', 13: 'Right On Time', 14: 'Around the World', 15: 'Purple Stain', 16: 'Under The Bridge'}}
def search():
    search = input("Which Album would you like to know about?")
    if rhcp_albums.get(search.lower()) == None:
        print("Invalid Search")
    else:
        print(rhcp_albums.get(search.lower()))

search()
{'title': 'Blood Sugar Sex Magik', 'artist': 'Red Hot Chili Peppers', 'year': 1991, 'genre': ['Rock', 'Funk', 'Alternative-rock'], 'tracks': {1: 'The Power of Equality', 2: 'If You Have to Ask', 3: 'Breaking the Girl', 4: 'Funky Monks', 5: 'Suck My Kiss', 6: 'I Could Have Lied', 7: 'Mellowship Slinky in B Major', 8: 'The Righteous & The Wicked', 9: 'Give It Away', 10: 'Blood Sugar Sex Magik', 11: 'Under the Bridge', 12: 'Naked in the Rain', 13: 'Apache Rose Peacock', 14: 'The Greeting Song', 15: 'My Lovely Man', 16: 'Sir Psycho Sexy', 17: "They're Red Hot"}}
for album in rhcp_albums.values():
    album["producer"] = "Rick Rubin"

print(rhcp_albums['californication']['producer'])
   
Rick Rubin
def get_albums_after_year(album_dict, year):
    """Return a list of album titles released after the given year."""
    albums_after_year = []
    for album in album_dict.values():
        if album['year'] > year:
            albums_after_year.append(album['title'])
    return albums_after_year

get_albums_after_year(rhcp_albums, 1998)
['Californication']
['Californication']
  • For Mr. Yeung's class: Justify your favorite Taylor Swift song, answer may effect seed
    • You Belong With Me