Dictionaries are one of the essential Python data types, and are useful for more than just storing list of items. For example, you can use them to store and sort all sorts of data, then use that data to power all sorts of projects. So, whether you need to create a notification system, or just store some values in an easy-to-access way, dictionaries are a good tool to have in your Programming toolbox.
How to create a dictionary in Python
To create a dictionary in Python, you first need to create an empty dictionary. You can then add keys and values to add items to it. The syntax is as follows:
dictionary_name = {} dictionary_name[key] = value It’s good practice to use lowercase letters when naming a key – this is so you don’t confuse it with a variable name. Once you’ve added values to your dictionary, you can change the values associated with those keys.
How to update a dictionary in Python
To update a dictionary in Python, you can use the same syntax as creating one, but add in the key and value you want to change:
dictionary_name[key] = new_value Entries can be deleted from a dictionary using the del keyword, but you need to specify the key in order to do so:
del dictionary_name[key] How to iterate through a dictionary in Python
Dictionaries can be quite useful for storing multiple items, but it can be tricky to get your head around how to iterate through them. This is because even though you can access the keys, there’s no obvious way to loop through the corresponding values.
Thankfully, there’s a way to do this. Whenever you access a key in a dictionary, it returns both the key and the value. So, you can use this to separate the two, then use a for loop to iterate through the values.
How to use a dictionary in Python
Dictionaries can be used in a variety of real-world projects, and it’s easy to see why – they’re a very versatile data structure. For example, you could use them to create a notification system using the nfty.sh service, which allows you to send notifications to Android and iOS devices.
To set up your notification system, you need to specify the content and settings for each notification. Rather than keeping these details in a file or hard-coding them into your code, you can use a dictionary to store all of the notification details. Nifty allows you to specify the following for each notification:
And that’s it for dictionaries in Python. As you can see, they’re a handy Data Storage object and a great introduction to Python programming. Thonny is a great way to get some hands-on experience with Python concepts, while learning more about dictionaries and other topics along the way. If you want to keep learning, there are a lot of great Python tutorials here on TechRadar. But if you want to get ahead of the game, why not check out some of the best programming languages on offer right now.
The creator of Nifty, a service that allows you to send notifications to Android and iOS devices via the nfty.sh service, has asked us to make clear that it is not a service endorsed or supported by Apple.















