How to Create Dictionary in Python

A Dictionary is a collection of key-value pairs.

To create a dictionary

1. Use the dict keyword  2. Place the key-value pairs inside curly braces "{ }"

--> Each key-value pair is separated by a comma  -->The key and value are separated by a colon

--> This code will create a dictionary called my_dict --> Add three key-value pairs to it -->Then, it will print the dictionary to the console. Output:

In this example, the keys are --> 'key1', 'key2', and 'key3' The corresponding values are  -->'value1', 'value2',and value3'

You can access the value associated with a particular key by using  square brackets [] and the key eg: --> my_dict['key2'] This returns the value associated with 'key2' key, which is 'value2'.

Check out the free Python Fundamentals for Beginners certification course to learn more.