How to create a dictionary in Python and make use of it

There are many builtin objects offered by the Python computer programming language which the Python coder can make use of while writing their Python based applications. Through this simple and short tutorial, you are going to learn how to create a dictionary object and how to make use of it in a practical case, in a real word scenario.

First of all, make sure to launch the Python interactive console on your own machine so you can run the code by yourself. I highly recommend doing this so everyone practices the knowledge being shared by codetheory.in.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Once you have managed to launch the Python interactive console in your own operating system, make use of the following syntax to declare a dictionary object.

d = {}

The above dictionary is an empty one. Based on my personal experience with the Python computer programming language, the dictionary object can be easily changed in specific points in time.

One can easily enter an element inside their dictionary by making use of the following syntax.

d[‘name’] = ‘oltjano’ # provide a name in here

Doing a simple analysis in the above example, one can easily tell that the elements inside a dictionary object are composed of a key and a value.

Based on the above piece of Python code, we can say the following.

# ‘name’ is the key
# ‘oltjano’ is the value

As far as my wisdom goes when it comes to the Python computer programming language, there is not a limit on the number of elements one can enter.

To make things more concrete let’s try to add some more elements inside the dictionary we have created, by using the following syntax.

d[‘age’] = 26
d[‘profession’] = ‘student’
d[‘passion’] = ‘blogging’
d[‘married’] = False
d[‘budget’] = 0.0
d[‘is_on_facebook’] = True

To take a look at the dictionary object we have created, we can easily type the following command.

d

Once the above piece of Python code gets executed on my Python interactive console, I get the following output.

{ ‘name’: ‘oltjano’,
‘married’: False,
‘profession’: ‘student’,
‘budget’: 0.0,
‘passion’: ‘blogging’,
‘is_on_facebook’: True
}

Now if you want to access a specific element in the dictionary object which you have in your interactive Python console, just follow the syntax shown below.

d[‘married’]

The output of the above command is shown below.

False

As you can see from the example showed above, one can easily access the values of the elements of their dictionary by using the corresponding key.

Final thoughts

The Python dictionary object is very useful to the coder, especially if they want to store data which needs to be presented in the key-value form.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *