Skip to main content

How to use dictionaries in Python

How to use dictionaries in Python

One of the first things any new developer should learn when they start Python, is how to create and store variables. These allow you to store and manipulate data, which is key to creating powerful and dynamic programs. One of the most powerful ways to store data in Python is with dictionaries. In this guide, you will learn how to create dictionaries, how to retrieve information, and how to add to a dictionary in Python!

Also read: How to round in Python

Basic commands

First, I’ll walk you through the statements you’ll need to create and access dictionaries. You’ll also learn how to add to a dictionary. Python makes all of this very easy! Then we’ll go over what this all means, and why you might want to use a dictionary over another method of storing values.

To create a new dictionary, you simply use curly brackets. You then enter your data, with the key and the value separated by a colon, and each entry separated by a comma:

newDict={"Jeff" : 7701489772,"Bill" : 7378999911, "Nancy" : 7711289354}

Note that Python will allow you to mix data types within your dictionary. Here, I have mixed strings and integers. This list stores a set of numbers with the person’s first name used as the key. This could be a handy way to quickly grab phone numbers of friends.

We can then retrieve the value based on the key:

print(newDict["Jeff"])

This will print Jeff’s number to the screen.

Updating entries is likewise very straightforward:

newDict["Jeff"] = 7789876224

And we can add to a dictionary in Python just as easily:

newDict["Claire"] = 7711176329

Finally, we can delete dictionary entries, clear the entire thing, or completely delete the dictionary entry:

Del newDict[“Claire”]

newDict.clear()

del newDict

When to use dictionaries in Python

That is how to add to a dictionary in Python and much more. But what is a dictionary useful for and why would you want to use one?

Essentially, dictionaries work a lot like lists. This means you can store lots of data in a single place for easy retrieval. We’ve previously discussed how to use lists in Python here:

The difference between a dictionary and a list however, is that lists are sequential and use numbers for their indexes. If you insert a new element into a list, this will change the position of all the other elements. That means that if we wanted to add new numbers to our list over time, we’d have to keep track of which one was which somehow. This could get complicated!

The beauty of a dictionary then, is that the data can be stored in any order, and new entries won’t disrupt old ones. Moreover, we know that the index will always remain consistent, and we can use any logical system we like for naming each entry. This is perfect for phone books, but also many other applications that just aren’t suited to lists.

To really get a handle on how to add to a dictionary in Python, how to store data in other ways, and how to do much more, you should consider taking an online course. There is a huge amount to learn when it comes to Python; and these are skills that can enhance your career, improve your workflow, and be extremely rewarding to develop. Check out our guide to the best online Python courses, and consider something like the comprehensive Ultimate Python Programmer & Data Certification Bundle. That particular course is currently discounted from $1,800, all the way to $39.



source https://www.androidauthority.com/how-to-add-to-a-dictionary-python-1136937/

Comments

Popular posts from this blog

LIVE Day Trading Morning Show for Tuesday!

LIVE Day Trading Morning Show for Tuesday! via YouTube https://www.youtube.com/watch?v=vgagRBXRJM0 Android Tips and Tricks working hard to get the latest and useful tips and techniques that help you to solve a problem or improve your productivity.

How to unhide or show folders in mx player list

In this blog post, I tell you about how to Show or Hide folders from MX player list. There are two methods to Show folder from MX Player list. Method 1: Unhide / show folders If you want to temporarily Show / Unhide hidden folder from MX Player list, then go to Settings and untick Recognize .nomedia ".  Method 2: Permanently unhide / show folder: Open memory by any file explorer and I recommend X-Plore, and open the folder that is Hidden and find the file " .nomedia ". If you didn't find it, you should first enable "Show files hidden files that starts with .(dot)". Delete the file and you just need to refresh MX Player list to take changes. Note: MX Player always hide those folders which file " .nomedia " exists.

How to use AWS – a simple guide for beginners

Amazon Web Services (AWS) is Amazon’s powerful, market-leading solution for cloud computing. The platform offers a suite of products for businesses: security, cloud backup, machine learning, IoT solutions, and more. In this post, we will explore how to use AWS. What you need to know Many entrepreneurs and small businesses may assume that AWS is not for them. Perhaps the pricing will be too prohibitive, or it will require too much technical know-how. While both of these issues certainly do crop up from time-to-time, the truth is that Amazon’s offerings are extremely wide-reaching and include options at many different price points and levels of complexity. That is to say, that while some products might be off-limits, others are not. Some AWS products are completely free and very simple to get to grips with! See also: AWS vs Azure vs Google Cloud – Which certification is best for professionals? AWS includes over 175 different products, some of which don’t even require an AWS accou...