person is the dictionary, and ['smarts'] is one of the values of a key, so it will print the value of smarts, which is 74.
If you just print person -
print(person)
it will give you this:
{"smarts":74,"looks":56,"power":49}
Lets say you wanted to format the print statement to print everything in a readable way. You would do this:
for value in person: #"value" is smarts, looks, and power
print(f'{value}: {person[value]}')
and the output would be:
smarts: 74
looks: 56
power: 49
so value is the name of the key, and person[value] is the value of that key.
and now... embedded dictionaries!
yes, you can have a dictionary inside of another dictionary.
things = {
"cars": { #cars is now a dictionary inside of things
"speed": 85,
"looks": 50,
}, #remember the comma!
"language": "python3", #language is not inside of cars
}
so to call something inside of an embedded dictionary, you do this:
car_speed = things['cars']['speed']
lets break this down car_speed: the variable we are assigning this to things: the dictionary with the value ['cars']: inside of the cars dictionary ['speed']: the value of speed
so... car_speed would equal 85.
Another cool way you can print the values is like this:
for x in things: # everything in things (cars, language)
if type(things[x]) == dict: #if it is a dictionary
print(f'{x} - ') #print it like this
for y in things[x]: #for everything in the dictionary (speed, looks)
print(f'\t{y}: {things[x][y]}') #print them like this
else: #if it's not a dictionary
print(f'{x}: {things[x]}') #print it like this
and the output is:
cars -
speed: 85
looks: 50
language: python3
you can have even MORE embedded dictionaries, such as:
Again, the values will be different.
Note: if the value of the key you want to copy is not int, change it to it, such as if it is a string, use str, or if it is a dictionary, use dict.
Removing keys
If you want to delete a key, use del.
dictionary = {"delete this": "please"}
del dictionary["delete this"]
And now, dictionary will have the value of {}.
If you have any questions or want me to explain anything more, I will be glad to.
Python dictionaries
One of the only times you need to use curly brackets in python...
Lets say you wanted to create values for something... lets say a person.
You can make a class, or you can make a dictionary.
here, lets clean this up a bit.
So now, we have a dictionary with the values of the person's smarts, looks, and power.
But how would you call one of these?
Like this:
person
is the dictionary, and['smarts']
is one of the values of a key, so it will print the value of smarts, which is 74.If you just print person -
it will give you this:
Lets say you wanted to format the print statement to print everything in a readable way. You would do this:
and the output would be:
so
value
is the name of the key, andperson[value]
is the value of that key.and now... embedded dictionaries!
yes, you can have a dictionary inside of another dictionary.
so to call something inside of an embedded dictionary, you do this:
lets break this down
car_speed
: the variable we are assigning this tothings
: the dictionary with the value['cars']
: inside of the cars dictionary['speed']
: the value of speedso...
car_speed
would equal 85.Another cool way you can print the values is like this:
and the output is:
you can have even MORE embedded dictionaries, such as:
Changing the values
so, what if you wanted to change a value inside of a dictionary?
and then, if you type "Hello, world!", the dictionary key
input 1
will be added with the value ofHello, world!
Copying dictionaries
Same as a list, if you do this:
It will also change the value of the original dictionary.
Instead, you need to use
.copy()
and now, the values will be different.
If you want to only copy a key/value in the dictionary and not the whole thing, do this:
Again, the values will be different.
Note: if the value of the key you want to copy is not
int
, change it to it, such as if it is a string, usestr
, or if it is a dictionary, usedict
.Removing keys
If you want to delete a key, use
del
.And now,
dictionary
will have the value of{}
.If you have any questions or want me to explain anything more, I will be glad to.