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.
person = {"smarts":74,"looks":56,"power":49}
here, lets clean this up a bit.
person = { "smarts":74, "looks":56, "power":49 }
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 = { "smarts":74, "looks":56, "power":49 } print(person['smarts']) # output: 74
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:
master_dict = { inside1: { inside2: { inside 3: { #and so on... }, }, }, }
Changing the values
so, what if you wanted to change a value inside of a dictionary?
user_input = {} #empty dictionary print(user_input) added_input = input('type something: ') user_input['input 1'] = added_input print(user_input)
and then, if you type "Hello, world!", the dictionary key input 1
will be added with the value of Hello, world!
{} type something: Hello, world! {'input 1': 'Hello, world!'}
Copying dictionaries
Same as a list, if you do this:
dict1 = {'key1': 'value'} dict2 = dict1 dict2['key1'] = 'new value'
It will also change the value of the original dictionary.
Instead, you need to use .copy()
dict1 = {'key1': 'value'} dict2 = dict1.copy() dict2['key1'] = 'new value'
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:
dict1 = {'key1': 'value','other key': 754} dict2['other key'] = int(dict1['other key']) dict2['other key'] += 90
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.
I have a question, how to Write a function that takes an integer value greater than 0, let's call it N, then outputs a dictionary that has keys 1 through N that have values that are N^2.
@azza1551
Can you be a little more specific? Also, you should probably post this question in replit Talk.
This is how you can get the first key of the dictionary.
student = { 'name': 'John', 'age': 22, 'gender': 'Male' } all_keys = list(student) print('All Keys: ', all_keys) # Get the first key using all_keys[0] print('First Key: ', all_keys[0])
@RhinoRunner Also, I've been working on a big python tutorial, do you want to help me? I will give you credit
@IntellectualGuy yeah sure, but how would I help?
@RhinoRunner Help with the content, Let me add you
@RhinoRunner Added You
Nice tutorial! Upvoted
@XThacker Thanks
@RhinoRunner np, you deserved it. I didn't know much bout dictionaries, now...I do.
@XThacker ah nice!
I'm also gonna make one about classes soon.
@RhinoRunner yay 😍