|
| 1 | +# A Python dictionary is a collection which is unordered, mutable, and indexed. In Python dictionaries |
| 2 | +# are written with curly brackets, and they have keys and values. |
| 3 | + |
| 4 | +# Making a dictionary |
| 5 | +player_0 = {'level': 'novice', 'points': 7} |
| 6 | + |
| 7 | +print(player_0['level']) |
| 8 | +# novice |
| 9 | + |
| 10 | +print(player_0['points']) |
| 11 | +# 7 |
| 12 | + |
| 13 | + |
| 14 | +# Using the get() method to access a value |
| 15 | +player_0 = {'level': 'novice'} |
| 16 | +player_level = player_0.get('level') |
| 17 | +player_points = player_0.get('points', 0) |
| 18 | +print(player_level) |
| 19 | +# novice |
| 20 | + |
| 21 | +print(player_points) |
| 22 | +# 0 |
| 23 | + |
| 24 | + |
| 25 | +# Dictionaries Inside Lists |
| 26 | +# Start with an empty list. |
| 27 | +profiles = [] |
| 28 | + |
| 29 | +# Make a new profile, and add them to the list. |
| 30 | +new_profile = { |
| 31 | + 'last': 'Diaz', |
| 32 | + 'first': 'Guillermo', |
| 33 | + 'profilename': 'gDiaz', |
| 34 | +} |
| 35 | +profiles.append(new_profile) |
| 36 | + |
| 37 | +# Make another new profile, and add them as well. |
| 38 | +new_profile = { |
| 39 | + 'last': 'Rezende', |
| 40 | + 'first': 'Pedro', |
| 41 | + 'profilename': 'pRezende', |
| 42 | +} |
| 43 | +profiles.append(new_profile) |
| 44 | + |
| 45 | +# Show all information about each profile. |
| 46 | +for profile_dict in profiles: |
| 47 | + for k, v in profile_dict.items(): |
| 48 | + print(f"{k}: {v}") |
| 49 | +print("\n") |
| 50 | +# last: Diaz |
| 51 | +# first: Guillermo |
| 52 | +# profilename: gDiaz |
| 53 | +# last: Rezende |
| 54 | +# first: Pedro |
| 55 | +# profilename: pRezende |
| 56 | + |
| 57 | +# Defining a list of dictionaries without using append() |
| 58 | +profiles = [ |
| 59 | + { |
| 60 | + 'last': 'Diaz', |
| 61 | + 'first': 'Guillermo', |
| 62 | + 'profilename': 'eDiaz', |
| 63 | + }, |
| 64 | + { |
| 65 | + 'last': 'Rezende', |
| 66 | + 'first': 'Pedro', |
| 67 | + 'profilename': 'mRezende', |
| 68 | + }, |
| 69 | +] |
| 70 | + |
| 71 | +# Show all information about each profile. |
| 72 | +for profile_dict in profiles: |
| 73 | + for k, v in profile_dict.items(): |
| 74 | + print(f"{k}: {v}") |
| 75 | +print("\n") |
| 76 | +# last: Diaz |
| 77 | +# first: Guillermo |
| 78 | +# profilename: eDiaz |
| 79 | +# last: Rezende |
| 80 | +# first: Pedro |
| 81 | +# profilename: mRezende |
| 82 | + |
| 83 | + |
| 84 | +# Adding a key-value pair |
| 85 | +player_0 = {'level': 'novice', 'points': 7} |
| 86 | +player_0['x'] = 0 |
| 87 | +player_0['y'] = 25 |
| 88 | +player_0['speed'] = 1.5 |
| 89 | + |
| 90 | +# The above code may also be written as a dictionary literal like so. |
| 91 | +player_0 = {'level': 'novice', 'points': 7, 'x': 0, 'y': 25, 'speed': 1.5} |
| 92 | + |
| 93 | +# Adding to an empty dictionary |
| 94 | +player_0 = {} |
| 95 | +player_0['level'] = 'novice' |
| 96 | +player_0['points'] = 7 |
| 97 | + |
| 98 | +# Modifying values in a dictionary |
| 99 | +# Change the player's level and point value. |
| 100 | +player_0['level'] = 'intermediate' |
| 101 | +player_0['points'] = 10 |
| 102 | +print(player_0) |
| 103 | +# {'level': 'intermediate', 'points': 10} |
| 104 | + |
| 105 | + |
| 106 | +# Removing key-value pairs |
| 107 | +player_0 = {'level': 'novice', 'points': 7} |
| 108 | + |
| 109 | +del player_0['points'] |
| 110 | +print(player_0) |
| 111 | +# {'level': 'novice'} |
| 112 | + |
| 113 | + |
| 114 | +# Storing lists in a dictionary |
| 115 | +fav_games = { |
| 116 | + 'Pedro': ['Outer Wilds', 'Disco Elysium'], |
| 117 | + 'Sean': ['Baba Is You'], |
| 118 | + 'Daniel': ['Sekiro', 'Star Wars Jedi'], |
| 119 | + 'Guillermo': ['Control', 'Dragon Quest Builders 2'], |
| 120 | +} |
| 121 | + |
| 122 | +# Show each name and associated games |
| 123 | +for name, games in fav_games.items(): |
| 124 | + print(f"{name}: ") |
| 125 | + for game in games: |
| 126 | + print(f"- {game}") |
| 127 | +# Pedro: |
| 128 | +# - Outer Wilds |
| 129 | +# - Disco Elysium |
| 130 | +# Sean: |
| 131 | +# - Baba Is You |
| 132 | +# Daniel: |
| 133 | +# - Sekiro |
| 134 | +# - Star Wars Jedi |
| 135 | +# Guillermo: |
| 136 | +# - Control |
| 137 | +# - Dragon Quest Builders 2 |
| 138 | + |
| 139 | +# Storing dictionaries in a dictionary |
| 140 | +profiles = { |
| 141 | + 'smcloughlin': { |
| 142 | + 'first': 'Sean', |
| 143 | + 'last': 'McLoughlin', |
| 144 | + 'gender': 'male', |
| 145 | + }, |
| 146 | + 'prezende': { |
| 147 | + 'first': 'Pedro', |
| 148 | + 'last': 'Rezende', |
| 149 | + 'gender': 'male', |
| 150 | + }, |
| 151 | +} |
| 152 | +for profilename, profile_dict in profiles.items(): |
| 153 | + print("\nUsername: " + profilename) |
| 154 | + full_name = profile_dict['first'] + " " |
| 155 | + full_name += profile_dict['last'] |
| 156 | + gender = profile_dict['gender'] |
| 157 | + print(f"\tFull name: {full_name.title()}") |
| 158 | + print(f"\tgender: {gender.title()}") |
| 159 | + |
| 160 | +# Username: smcloughlin |
| 161 | +# Full name: Sean Mcloughlin |
| 162 | +# gender: Male |
| 163 | + |
| 164 | +# Username: prezende |
| 165 | +# Full name: Pedro Rezende |
| 166 | +# gender: Male |
| 167 | + |
| 168 | + |
| 169 | +# Looping through all key-value pairs |
| 170 | +# Store people's favorite games. |
| 171 | +fav_games = { |
| 172 | + 'George': 'Crash Bandicoot', |
| 173 | + 'Alex': 'Super Smash Bros', |
| 174 | + 'Sarah': 'Legend Of Zelda', |
| 175 | + 'Allison': 'Pong', |
| 176 | +} |
| 177 | + |
| 178 | +# Loop over key and value |
| 179 | +for name, game in fav_games.items(): |
| 180 | + print(f"{name}: {game}") |
| 181 | +# George: Crash Bandicoot |
| 182 | +# Alex: Super Smash Bros |
| 183 | +# Sarah: Legend Of Zelda |
| 184 | +# Allison: Pong |
| 185 | + |
| 186 | + |
| 187 | +# Looping through all the keys |
| 188 | +for name in fav_games.keys(): |
| 189 | + print(name) |
| 190 | +# George |
| 191 | +# Alex |
| 192 | +# Sarah |
| 193 | +# Allison |
| 194 | + |
| 195 | +# Looping through all the values |
| 196 | +for game in fav_games.values(): |
| 197 | + print(game) |
| 198 | +# Crash Bandicoot |
| 199 | +# Super Smash Bros |
| 200 | +# Legend Of Zelda |
| 201 | +# Pong |
| 202 | + |
| 203 | +# Looping through all the keys in reverse order |
| 204 | +for name in sorted(fav_games.keys(), reverse=True): |
| 205 | + print(f"{name}: {fav_games[name]}") |
| 206 | +# Sarah: Legend Of Zelda |
| 207 | +# George: Crash Bandicoot |
| 208 | +# Allison: Pong |
| 209 | +# Alex: Super Smash Bros |
| 210 | + |
| 211 | +# Checking The Length Of A Dictionary |
| 212 | +num_responses = len(fav_games) |
| 213 | +print(num_responses) |
| 214 | +# 4 |
| 215 | + |
| 216 | + |
| 217 | +# Using a loop to create a dictionary |
| 218 | +cubes = {} |
| 219 | +for x in range(5): |
| 220 | + cubes[x] = x ** 3 |
| 221 | +print(cubes) |
| 222 | +# {0: 0, 1: 1, 2: 8, 3: 27, 4: 64} |
| 223 | + |
| 224 | +# Using a dictionary comprehension |
| 225 | +cubes = {x: x ** 3 for x in range(5)} |
| 226 | +print(cubes) |
| 227 | +# {0: 0, 1: 1, 2: 8, 3: 27, 4: 64} |
| 228 | + |
| 229 | +# Using zip() to make a dictionary |
| 230 | +veggies = ['pepper', 'broccoli', 'spinach', 'potato'] |
| 231 | +fruits = ['apple', 'orange', 'peach', 'plum'] |
| 232 | +combos = {key: key_2 for key, key_2 in zip(veggies, fruits)} |
| 233 | +print(combos) |
| 234 | +# {'pepper': 'apple', 'broccoli': 'orange', 'spinach': 'peach', 'potato': 'plum'} |
| 235 | + |
| 236 | +# To create a large number of dictionaries at once, you can use a loop like so. |
| 237 | +players = [] |
| 238 | +# Make a thousand novice players, worth 3 points |
| 239 | +# each. Have them all start in one row. |
| 240 | +for player_num in range(1000): |
| 241 | + new_player = {} |
| 242 | + new_player['level'] = 'novice' |
| 243 | + new_player['points'] = 3 |
| 244 | + new_player['x'] = 20 * player_num |
| 245 | + new_player['y'] = 0 |
| 246 | + players.append(new_player) |
| 247 | + |
| 248 | +# Show the list contains a thousand players. |
| 249 | +num_players = len(players) |
| 250 | + |
| 251 | +print("Number of players created:") |
| 252 | +# Number of players created: |
| 253 | + |
| 254 | +print(num_players) |
| 255 | +# 1000 |
0 commit comments