Tuesday, March 26, 2024

Dictionaries

 

Dictionaries
As you might have guessed, we love lists. However, list items are accessed in order by index number. This isn't always the way we want it to work.

Dictionaries are a slightly different type of list that access data by giving each item a key. This creates what we call key:value pairs.

Now we can access each item through its key, instead of having to remember what index it is at in the list.

Creating a dictionary - brace!
Curly, curly braces...
To create a dictionary we start just like a list, except with curly braces {}. This dictionary will store data about a user.
The data is inserted in key value pairs like this. Each pair is separated by a comma:


The first key:value pair below has "name" as the key and "David" as the value. Try it out:

myUser = {"name": "David", "age": 128}

Printing the keys
To output (print) from a dictionary, we can use the key instead of the index. Note that we still use square brackets for accessing items (ex: ["name"]).

Let's print "name".

myUser = {"name": "David", "age": 128}
print(myUser["name"])

# This code outputs 'David'.

Changing an item
You can use the = syntax to change key values.

myUser = {"name": "David", "age": 128}

myUser["name"] = "The legendary David"
print(myUser)

# This code outputs 'name:'the legendary David', 'age':'128.

Note that we have to put the keys in single quotation marks '' inside the fString when using this technique.
This is because we've already used double quotes to start and end the fString. So, using "" for the dictionary value would get Python all confuzzled.


myUser = {"name": "David", "age": 128}

print(f"Your name is {myUser['name']} and your age is {myUser['age']}")

# This code outputs 'Your name is David and your age is 128'.

Syntax error?

Why are you getting a syntax error on the print statement line?
The print statement uses square brackets. Curly braces {} are only used to call the value.

Wrong 
myUser = {"name": "David", "age": 128}

print(myUser{"name"})

Correct 
myUser = {"name": "David", "age": 128}

print(myUser["name"])

Undefined?

The key, name, in the dictionary should be inside quotes.

Wrong
myUser = {name: "David", "age": 128}

print(myUser["name"])


Correct 
myUser = {"name": "David", "age": 128}

print(myUser["name"])

Spare Key?
A dictionary can't have two keys with the same name. It always overrides the previous one. Therefore, the 129 overrides the age, 128.

Wrong
myUser = {name:"David", "age": 128, "age" = 129}

print(myUser)

Correct 
myUser = {"name":"David", "age": 128}

myUser["age"] = 129

print(myUser)


Coding Challenge 
Ask the user to input their name, date of birth, telephone number, email and physical address.
Store it all in a dictionary.
Print it out in a nice way once its stored.

x = input("Input your name >")
y = input("Input your date of birth >")
z = input("Input your telephone number >")
a = input("Input your email >")
b = input("Input your address >")


myUser = {"name":x, "DOB": y , "phone": z, "email": a, "address": b}

print(f"Hi {myUser['name']}. Our dictionary says that you were born on {myUser['DOB']}, we can call you on {myUser['phone']}, email {myUser['email']}, or write to {myUser['address']}.")

print(myUser)

  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...