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

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...