How To Add To A Dictionary In Python

In this tutorial, we will know to add to a Dictionary in Python. Coding languages rely on intense and interpretive coding to follow through on functions and keep websites.

Other projects that they are used for running.

The unfortunate thing about coding is that it is confusing and complex, sometimes even for the systems themselves.

If you write one little error into your code, the whole data set you just wrote might simply not work.

Then you have to work through thousands of lines of code to find the error.

As such, coding languages have specific data structures that can be implemented in order to organize and store collections of usable data.

One of these is a dictionary, but how exactly do you add to a dictionary within python? Is it easy or a hard thing to do?

What Is A Dictionary In Python?

A dictionary is a data structure that stores elements inside a system as key-value pairs. These key-value pairs are saved in memory and can be searched, modified and deleted in any order.

The data was unordered before the newer versions of python came out with the order.

That the keys are imputed in not necessarily reflecting the order that they are reported back in.

But this has since changed to them being ordered as they were input.

So, a dictionary basically consists of a list of pairs of data.

The first entry of each pair is the key of the pair, and the second one is the value.

The only rule is that the key must be unique, and it must only be entered into the dictionary once.

A dictionary is a very useful data structure when you have a large amount of data that has a certain order, like a phone book, statistics, a dictionary, etc.

Also read: Python Match Case with Code Example

How To Create A Dictionary?

To create a dictionary, you must nestle a key and value pair within curly brackets and have a name for it before the brackets.

The dictionary name can be anything you wish to call the dictionary itself.

For example, if we were to create a dictionary about an actor, like Sandra Bullock.

We would write ‘SandraBullock’ as the starting point before adding an equal’s sign and a curly bracket at the end, like so: ‘SandraBullock = {‘. On the next line of your coding editor, we would add the key and value pairs.

If the dictionary was created as a biography of Sandra Bullock, we would input the necessary components of the biography. For this example, we will have the keys be her name, age, and nationality, like so:

'SandraBullock = {

"Name": "Bullock",

"Age": 57,

"Nationality": "American",

}

Print(SandraBullock)
'

Program Returns:

{‘Name’: ‘Bullock’, ‘Age’: 57, ‘Nationality’: “American”}

Each key of a dictionary would have a value that corresponds to it, above we can see that “Name” corresponds to “Sandra”.

The commas after each pair separates them from the next one, if you leave out the comma you are asking for an error to appear and throw the whole system out.

Features Of A Dictionary

While dictionaries seem straightforward and similar to other data structures, they do have some unique features that define them and set them apart from others as well:

No duplicate keys: the keys of any python dictionary need to be unique and cannot be duplicated. If you were to write:

'SandraBullock = {

"Name": "Bullock",

"Name": "Annette",

"Name": "Sandra",

}'

Then as soon as you printed to the console, the very last name key would overwrite the rest, and it would show: ‘Sandra Sandra Sandra’.

Values are changeable: When you have completed and assigned things to a dictionary, you can alter the value of the item to something that is totally different. If you were to write:

'SandraBullock = {

"Name": "Bullock",

"Age": "57",

"Nationality": "American",

}

SandraBullock["age"] = 56

Print(SandraBullock)
'

Program Returns:

{‘Name’: ‘Bullock’, ‘Age’: 57, ‘Nationality’: “American”}

You can see, we have reassigned age a different value, which will now override the initial value that we assigned the dictionary when it was made.

The values can also be changed using the update method – update() – though this requires writing a slightly longer piece of code. For example, ‘SandraBullock.update({“age”:56})’.

Items are now ordered: Previously, python dictionaries weren’t ordered, but now they are, with the items ordered in the dictionary in the same order that they were created or inputted in. The order for dictionaries cannot nor should it be changed.

While items can be added, the order must stay the same.

How To Add To A Dictionary In Python?

The method and language for adding a new item in a python dictionary is the same as when you are updating an item within the dictionary.

The real difference comes from the index key, which will include the name of the new key and the name of the new value. This would look like:

'SandraBullock[newkey] = newvalue'

As before, you can also use the update method and, as before, it can be a little longer than the normal code:

'SandraBullock.update({"newkey":newvalue})'.

If we were to use our prior example from before, then we can see this code in action:

'SandraBullock = {

"Name": "Bullock",

"Age": "57",

"Nationality": "American",

}

SandraBullock["job"] = "Actress"

Print(SandraBullock)
'

Program Returns:

{‘Name’: ‘Bullock’, ‘Age’: 57, ‘Nationality’: “American”}

And now with the ‘update()’ example of adding to the dictionary:

'SandraBullock = {

"Name": "Bullock",

"Age": "57",

"Nationality": "American",

}

SandraBullock.update({"Job": "Actress"})

Print(SandraBullock)
'

Program Returns:

{‘Name’: ‘Bullock’, ‘Age’: 57, ‘Nationality’: “American”}

Even though it can be confusing at first, as you can see once you get the hang of it, it can be really easy.

Conclusion

Dictionaries can be a bit confusing at first, but once you get the hang of them, you’ll find that they’re very useful and powerful data structures.

Like all other data structures, they have their pros and cons, and they’re not appropriate for every situation.

When you’re writing code, make sure you know which data structure makes the most sense for the situation at hand, and make sure you know how to use that data structure properly before implementing it.

Leave a Comment