Dictionary in Python

Dictionary in Python

In the dictionary, you can store your data as a key-value method. Each entry can have a key and a value or can have another dictionary or list inside that entry. We separate dictionary entries by commas (,) between them. We separate key-value by a colon (:) between them.

Keys must be unique while values can be repeated strings or numbers. In the key, you can include only strings, numbers, tuples types of variables while in the value you can include any type you want such as a list or another dictionary.

Dictionary Syntax

#____ProgrammerCodes.com____
dictionaryName = {Key:Value,Key:Value,Key:Value}
#____ProgrammerCodes.com____

Example Of A Dictionary In Python

#____ProgrammerCodes.com____
students_age = {"Oliver":17,"Charlotte":16,"Eva":17,"James":16,"David":16}
print(students_age)
#____ProgrammerCodes.com____

Result

{'James': 16, 'Eva': 17, 'David': 16, 'Oliver': 17, 'Charlotte': 16}

How To Access A Dictionary In Python

To Access a key in a dictionary and print or do a programming process with its data, you have to write the name of the dictionary then include only the name of the key.

Syntax

#____ProgrammerCodes.com____
dictionaryName[key]
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
students_age = {"Oliver":17,"Charlotte":16,"Eva":17,"James":16,"David":16}
print(students_age["Eva"])
#____ProgrammerCodes.com____

Result

17

How To Update A Dictionary In Python?

In order to update a key-value in a dictionary, you have to write the name of the dictionary with the name of the key that you want to change and add an equal sign (=) then write the new value.

Syntax

#____ProgrammerCodes.com____
dictionaryname[key] = Write The New Value Here
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
students_age = {"Oliver":17,"Charlotte":16,"Eva":17,"James":16,"David":16}
print("Old Dictionary: ", students_age)
students_age["Oliver"] = 15
print("New Dictionary: ", students_age)
#____ProgrammerCodes.com____

Result

Old Dictionary: {'David': 16, 'Charlotte': 16, 'James': 16, 'Oliver': 17, 'Eva': 17}
New Dictionary: {'David': 16, 'Charlotte': 16, 'James': 16, 'Oliver': 15, 'Eva': 17}

How To Delete A Dictionary Item In Python?

If you want to delete something in a dictionary, you will have 3 options:

  • del dictionaryName[key] : delete only a dictionary item or entry.
  • del dictionaryName : delete the entire dictionary so it won’t be exist in the program.
  • dictionaryName.clear() : delete all dictionary keys and values but the dictionary itself will still be there in the program so you can add new key-value to it.

del dictionaryName[key]

Example

#____ProgrammerCodes.com____
students_age = {"Oliver":17,"Charlotte":16,"Eva":17,"James":16,"David":16}
print("Old Dictionary: ", students_age)
del students_age["Charlotte"]
print("New Dictionary: ", students_age)
#____ProgrammerCodes.com____

Result

Old Dictionary: {'Eva': 17, 'James': 16, 'Oliver': 17, 'David': 16, 'Charlotte': 16}
New Dictionary: {'Eva': 17, 'James': 16, 'Oliver': 17, 'David': 16}

dictionaryName.clear()

Example

#____ProgrammerCodes.com____
students_age = {"Oliver":17,"Charlotte":16,"Eva":17,"James":16,"David":16}
print("Old Dictionary: ", students_age)
students_age.clear()
print("New Dictionary: ", students_age)
#____ProgrammerCodes.com____

Result

delete all dictionary keys and valuesOld Dictionary: {'David': 16, 'ChAarlotte': 16, 'James': 16, 'Eva': 17, 'Oliver': 17}
New Dictionary: {}

del dictionaryName

Example

#____ProgrammerCodes.com____
students_age = {"Oliver":17,"Charlotte":16,"Eva":17,"James":16,"David":16}
print("Old Dictionary: ", students_age)
del students_age
print("New Dictionary: ", students_age)
#____ProgrammerCodes.com____

Result

Old Dictionary: {'Oliver': 17, 'Charlotte': 16, 'Eva': 17, 'David': 16, 'James': 16}
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\python\test.py", line 7, in
print("New Dictionary: ", students_age)
NameError: name 'students_age' is not defined

We got an error because we have deleted the entire dictionary so the program does not know about our dictionary. It is not stored in the memory.

Tutorial’s Question

How To Delete A Dictionary Item In Python programming language?

  • del dictionaryName[key]
  • del dictionaryName
  • dictionaryName.clear()

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Leave a Comment

Your email address will not be published. Required fields are marked *