Load data in dictionary Python

that structures its data with a key and value pair. It is mutable and does not allow duplicate values. In this article, we are going to cover how to create a dictionary, update and delete keys and also a little bit about how we use it in the real world.



Creating a Python dictionary

The first step involves choosing the name of the Python dictionary and assigning it to a variable. In this article, we’ll create a dictionary by the name “elephant” and then we need to equate this name to a pair of curly brackets which in Python dictate that this is going to be a dictionary.



Within the curly braces, we are going to create a pair of elements separated by a colon, the first one being the Key and the second one being the Value as shown here: “Key : Value”. While values can be of any data type and allow duplicates  Keys are immutable and cannot be duplicated. The “elephant” dictionary can be created as shown below, however, we are going to split the keys and values into separate lines for simplicity and ease in reading.



Now both numbers and strings can be keys in this case and the values can be whatever you like. Each pair is separated by a comma to add another pair hit enter and type it in the next line. “Age” is the next key with a corresponding value of 25 which is then followed by “weight” : 4000 and “type”: “African” pairs each occupying their separate lines. This is how a Python dictionary is created; we have four separate keys associated with four values.

Referencing a specific key

Items in a Python dictionary can be accessed by referring to their keys inside square brackets, we can then output their values using the print statement. As shown in the code below.



Deleting and updating elements in a Python dictionary

But what if we wanted to remove a key or value from this dictionary? Well we can do that quite simply by doing “del” for delete and then calling the dictionary we want to delete the value from and again the square brackets and then the key that we want to delete.



We are going to print the whole dictionary beforehand and then afterwards and we’ll see what has been removed from the dictionary. Suppose we want to delete the key “age” and its respective value we can do so by running the code del elephant[‘age’].

Now if we proceed to print out the dictionary again we can see that it has been removed.

We can also add elements to an existing Python dictionary using the built-in method “.update()”.This can be done nicely and simply by just running this code elephant.update() and passing the pair of “Key : Value” that we wish to update. As illustrated in the code below.


In the output above the first line is the original dictionary before we made any changes, the second line is the output after we have removed age from the dictionary. Finally, the last line is the output after updating the age from 25 to 20.

Please note that in the last output the code is not able to find the key “age” that needs to be updated since we had deleted it in the preceding step. However, the code proceeds to add it like a new pair.



Creating a list of Dictionaries

In the real world of programming more often than not you’ll encounter a list of dictionaries. As much as they might seem more complex please note that they are quite simple. Using the preexisting dictionaries, let’s add another elephant by the name: “Jessica”, “age”: 20 with a “weight” of 3500 and of Indian type.

To print elements from either of the dictionaries is a bit different, but it simply involves using two square brackets, the first one defining the dictionary to look up information while the second one references a specific key in that particular Python dictionary. As illustrated below.




This is how a Python dictionary works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

  • How to use the Python for loop
  • How to use Python Sets
  • How to use a Python Dictionary
  • How to use Python Classes
  • How to use Python Range
  • How to use Python if-else statements
  • How to use Python RegEx
  • How to use Python Lists
  • How to use Python Enumerate
  • How to use Python Functions
  • How to use Python Split
  • How to use Python Try-Except
  • How to use Python Tuples
  • How to use Python Arrays
  • How to use Python Sort
  • How to use the Python DateTime
  • How to download Python?
  • How to use the Python FileWrite function
  • How to use Python Lambda
  • How to use Python ListAppend
  • How to use Python ListComprehension
  • How to use Python Map
  • How to use Python Operators
  • How to use Python Pandas
  • How to use Python Requests
  • How to use Python Strings
  • How to use Python Count
  • How to use Python Comments
  • How to use the Python File Reader method
  • How to use the Python IDE-s
  • How to use Python logging
  • How to use Python Print
  • How to use the Python Zip
  • How to use Python Append
  • How to use Python Global Variables
  • How to use the Python join method
  • How to use Python list length
  • How to use Python JSON files
  • How to use Python Modulo
  • How to use Python file opening methods
  • How to use Python round
  • How to use Python sleep
  • How to use Python replace
  • How to use Python strip
  • How to use the Python Time module
  • How to use Python unittests
  • How to save data to a text file using Context Manager?
  • How to use Python external modules
  • How to use Python find
  • How to use the Python pip package manager
  • How to delete files in Python
  • Parsing XML files in Python
  • How to make a GUI in Python
  • How to use Python in Command Prompt
  • How to Run a Python Program in VS Code
  • How to run a program in Python IDLE
  • How to run a program in Jupyter Notebook
  • How to read a text file in Python
  • How to add numbers in Python
  • How to ask for user input in Python
  • How to debug in Python
  • How to create a thread in Python
  • How to import a library in Python
  • How to use the PIP package manager
  • How to use classes in Python
  • How to reverse strings in Python
  • How to convert a string to int in Python
  • How to print on the same line in Python
  • How to remove items from a list
  • How to add to a dictionary in Python
  • How to raise an exception in Python
  • How to throw an exception in Python
  • How to stop a program in Python
  • How to use Python assert
  • How to use the Python compiler

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

How to load data into dictionary in Python?

There is no add() , append() , or insert() method you can use to add an item to a dictionary in Python. Instead, you add an item to a dictionary by inserting a new index key into the dictionary, then assigning it a particular value.

How do you put data into a dictionary?

Let's see how can we add new keys to a dictionary using different ways to a dictionary..
Create a dictionary first..
Method 1: Add new keys using the Subscript notation..
Method 2: Add new keys using update() method..
Method 3: Add new keys using the __setitem__ method..

How to store data in dictionary Python?

To create a dict, we use syntax like the following..
example = {} type(example).
example['key'] = 'value' example['key'].
example[1] = 2 example[4] = False example['test'] = 5 example[7] = 'myvalue'.
list(example. keys()).
list(example. values()).

How to read data from a dictionary in Python?

In Python, you can get the value from a dictionary by specifying the key like dict[key] . In this case, KeyError is raised if the key does not exist.