Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python dictionaries
RhinoRunner

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, and person[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 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:

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 of Hello, 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, use str, or if it is a dictionary, use dict.

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.

Voters
TaguhiChakhoyan
programmeruser
IntellectualGuy
RhinoRunner
Comments
hotnewtop
[deleted]

Nice tutorial! Upvoted

RhinoRunner

@XThacker Thanks

[deleted]

@RhinoRunner np, you deserved it. I didn't know much bout dictionaries, now...I do.

RhinoRunner

@XThacker ah nice!
I'm also gonna make one about classes soon.

[deleted]

@RhinoRunner yay 😍

ManojKumar158

This is how you can get the first key of the dictionary.

source: How to get the first key of a dictionary in Python

IntellectualGuy

Noice

Updooted

RhinoRunner

@IntellectualGuy

Noice

Thanks

IntellectualGuy

@RhinoRunner Also, I've been working on a big python tutorial, do you want to help me? I will give you credit

RhinoRunner

@IntellectualGuy yeah sure, but how would I help?

IntellectualGuy

@RhinoRunner Help with the content, Let me add you

RhinoRunner
IntellectualGuy

@RhinoRunner Added You