How To Create A Dictionary In Python

In this tutorial, you will learn to create a dictionary in Python.

A dictionary is a data structure for storing data in the form of a key-value pair. Dictionary keys are unique, and these keys will have some sort of associated value.

Let us see in the below example code everything related to Dictionary in Python.

How To Create A Dictionary

You can create a Dictionary [1] in Python by equating any variable with curly braces. Let us see the syntax below.

Syntax:

dictionary_variable = {key:value}

Note: Key = Any data but required to be unique.
      Value = Any Data representing a value (may or may not be unique).

Creating An Empty Dictionary In Python

Let us create an empty dictionary in Python in the below code example.

#Initializing the Empty Dictionary
newDict = {}

#Printing the Dictionary
print(newDict)

Output:

{}

Explanation:

  • In the Above code, once you will execute it you will find that empty curly braces are printed to represent an empty dictionary.

Adding Elements To Dictionary One By One

In this next step, you will learn how to add elements to the dictionary one by one in the below example code.

#How to Create An Empty Dictionary
newDict = {}

#Adding New Element to Dictionary
newDict[1] = 'A'
newDict[2] = 'B'


#Printing the Dictionary
print(newDict)

Output:

{1: 'A', 2: 'B'}

Explanation:

  • I have added two key-value pair in the above dictionary, for key 1 I am storing a value as ‘A’ and for the key 2 I am storing the value as ‘B’.
  • And now I am printing the updated Dictionary, I received a dictionary with the key-value pairs added above.

How To Initialize A Non Empty Dictionary

If you want to initialize a non-empty dictionary then all you are required to do is put the key-value pair as per the syntax shown above. Note that since keys need to be unique while initializing make sure that you are using the unique keys.

Let us see in the below example code to initialize a non-empty dictionary.

#How to Create A Non-Empty Dictionary
newDict = {1: 'C', 2:'D', 3: [4,5,6], 'A': 7}

#Printing the Dictionary
print(newDict)

Output:

{1: 'C', 2: 'D', 3: [4, 5, 6], 'A': 7}

Explanation:

  • As you can see in the above code, I have initialized the non-empty dictionary with various different types of key-value pair that you can store in the dictionary.
  • You can have a integer as key and string as value, string as key and list as value.
  • It means that a Dictionary is not binded by one type of data type and you can use any data types to create the key-value pairs.

How To Update A Key-Value Pair In Dictionary

If you want to update any specific key-value pair in the Dictionary then all you are required to do is use the key in the square braces and then update the value to whichever value you want.

Let us see in the below example code to update the key-value pair in Dictionary in Python.

#How to Create A Non-Empty Dictionary
newDict = {1: 'C', 2:'D', 3: [4,5,6], 'A': 7}

#Printing the Dictionary
print("Dictionary Before Update\n {}".format(newDict))

#Update Key-Value Pair
newDict[3] = 'E'

#Printing the Dictionary
print("Dictionary After Update\n {}".format(newDict))

Output:

Dictionary Before Update
 {1: 'C', 2: 'D', 3: [4, 5, 6], 'A': 7}
Dictionary After Update
 {1: 'C', 2: 'D', 3: 'E', 'A': 7}

Explanation:

  • As you can see in the above code, I have used the key 3 to update its value from a list of numbers to a character ‘E’.
  • So, all you are required to do is use the square braces with the key and then update the value you want.
How To Create A Dictionary In Python

Wrap Up

I hope you learned how to create a dictionary in Python. I have also listed down various methods to update and add key-value pairs in the Python Dictionary.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Get First Key From Dictionary Python – 3 Methods
  2. How To Check If A Number Is Even In Python
  3. How To Change A Variable Outside Of A Function In Python
  4. How To Compare String and Integer in Python

Leave a Comment