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

4 Surprising Ways Artificial Intelligence will Empower Consumers

Ever wonder what makes Siri search the items inside the iPhone and on the web by recognizing your voice? How does Google listening work? Artificial Intelligence and data science have already infused in this consumer-oriented generation, but we have not realized it yet. It has been quite a while that natural language processing, speech recognition, and gesture recognition system took over the market with their all new features, but what are the new things in AI that are yet to come? Few amazing transformations are: Retailing sector to become AI-ready Voice Recognition will change the game Search engines to become smarter Machine learning to bring revolutionary changes Recently we got to learn about a verbal spat between Elon Musk and Mark Zuckerberg over the probable risk and opportunities brought by AI . Believe it or not, but we already accustomed to Artificial Intelligence and probably waiting to witness jaw-dropping inventions. Some of AI inventions we have known till yet are: Mach

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 arrays in Python

Arrays in Python give you a huge amount of flexibility for storing, organizing, and accessing data. This is crucial, not least because of Python’s popularity for use in data science. But what precisely is an array? And how do you use arrays in Python? Also read: How to use dictionaries in Python Read on, and we’ll shed some light on the matter. What is an array? An array is a way to store multiple values in a single variable. That means that you can use a single “reference” in order to access your data. A list is also an example of a variable that stores multiple values, but has some slight differences. When using lists in Python, you store a series of values each with a numbered index. For example, this is how you would create a list of fruits in Python: fruits = [“apple”, “orange”, “pear”, “nectarine”] If we then say: print(fruits[3]) We will see “nectarine” appear on the screen (the first entry is stored as “0”). Also read: How to use lists in Python This is not an