Learn about Dictionaries datatype for Python

You are here: Home » Blog » Computer Programming Languages » Python » Learn about Dictionaries datatype for Python

Learn about Dictionaries datatype for Python

Learn about Dictionaries datatype for Python: A dictionary is a data type similar to arrays but works with keys and values instead of indexes. Each value stored in a dictionary can be accessed using a key, which is any type of object (a string, a number, a list, etc.) instead of using its index to address it.

For example, a database of phone numbers could be stored using a dictionary like this:

phonebook = {}
phonebook["John"] = 555477566
phonebook["Jack"] = 555377264
phonebook["Jill"] = 555662781
print(phonebook)

Trinket.io on-line Python compiler

Alternatively, a dictionary can be initialized with the same values in the following notation:

phonebook = {
    "John" : 938477566,
    "Jack" : 938377264,
    "Jill" : 947662781
}
print(phonebook)

Trinket.io on-line Python compiler

Iterating over dictionaries

Dictionaries can be iterated over, just like a list. However, a dictionary, unlike a list, does not keep the order of the values stored in it. To iterate over key-value pairs, use the following syntax:

phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781}
for name, number in phonebook.items():
    print("Phone number of %s is %d" % (name, number))

Trinket.io on-line Python compiler

Removing a value

To remove a specified index, use either one of the following notations:

phonebook = {
   "John" : 938477566,
   "Jack" : 938377264,
   "Jill" : 947662781
}
del phonebook["John"]
print(phonebook)

Trinket.io on-line Python compiler

or:

phonebook = {
   "John" : 938477566,
   "Jack" : 938377264,
   "Jill" : 947662781
}
phonebook.pop("John")
print(phonebook)

Trinket.io on-line Python compiler

Exercise

Add “Jake” to the phonebook with the phone number 938273443, and remove Jill from the phonebook.

#this is the answer code. use online compiler below to complete the assignment on your own.
phonebook = {
    "John" : 938477566,
    "Jack" : 938377264,
    "Jill" : 947662781
}

# write your code here
phonebook["Jake"] = 938273443
del phonebook["Jill"]

# testing code
if "Jake" in phonebook:
    print("Jake is listed in the phonebook.")
if "Jill" not in phonebook:
    print("Jill is not listed in the phonebook.")

Trinket.io on-line Python compiler

 

Related Videos:

MiltonMarketing.com Related Videos.

Related Posts:

Comparing Python to other languages

Learn RE – Regular Expressions in Python

Python Serialization (JSON)

Exception Handling in Python programming

Learn Python Lists

Learn about Python Generators

About the Author:

I am a loving father, & husband. I am a computer enthusiast. I have used and enjoyed computers since I was young and I enjoy teaching young minds how to code, because it teaches them how to think. Today with YouTube, and social media garbage our youth are losing the ability to think on their own and solve problems. I believe this is a serious epidemic as kids today dont understand that technology is a tool. This tool is being abused, and its underlying effects are taking its toll on kids behaviour, and learning.