Python Tutorial - Dictionary Basic Operations

Since the dictionary is a variable sequence, we can manipulate key-value pairs in the dictionary. There are several common dictionary operations in Python:

  • Add new key-value pairs to an existing dictionary.
  • Modify key-value pairs in an existing dictionary.
  • Removes the specified key-value pair from the existing dictionary.
  • Determines whether the specified key-value pair exists in the existing dictionary.

Beginners should keep in mind that the dictionary is composed of key-value one by one. The key is the key to find the data. Python's operations on the dictionary are performed by key.

Python Dictionary - Add Key-Value Pairs

Adding new key-value pairs to the dictionary is very simple. You can directly assign a value to a key that does not exist. The specific syntax is as follows:

dictname [key] = value

Explanation of each part:

  • dictname represents the dictionary name.
  • key represents the new key.
  • value represents the new value, as long as it is a data type supported by Python.

The following code illustrates the process of adding new elements to an existing dictionary:

age = {'Jason': 95}
print (age)
#Add new key-value pair
age ['Nicole'] = 29
print (age)
#Add new key-value pairs again
age ['Harry'] = 10
print (age)

The output is:

{'Jason': 95}
{'Jason': 95, 'Nicole': 29}
{'Jason': 95, 'Nicole': 29, 'Harry': 10}

Python Dictionary - Modify Key-Value Pairs

The name of the key in the Python dictionary cannot be modified, we can only modify the value.

The key of each element in the dictionary must be unique, so if the key of the newly added element is the same as the key of the existing element, the value corresponding to the key will be replaced by the new value, so as to achieve the purpose of modifying the value of the element . Look at the following code:

age = {'Jason': 95, 'Nicoe': 29, 'Harry': 10}
print (age)
age ['Harry'] = 80
print (age)

The output is:

{'Jason': 95, 'Nicoe': 29, 'Harry': 10}
{'Jason': 95, 'Nicoe': 29, 'Harry': 80}

It can be seen that instead of adding another {'Harry': 80} key-value pair to the dictionary, the value in the original key-value pair {'Harry': 10} has been modified.

Python Dictionary - Delete Key-Value Pairs

If you want to delete the key-value pairs in the dictionary, you can still use the del statement. Example:

# Use the del statement to delete key-value pairs
age = {'Jason': 95, 'Nicole': 29, 'Harry': 10}
del age ['Nicole']
del age ['Harry']
print (age)

The output is:

{'Jason': 95}

Determines whether a specified key-value pair exists in the python dictionary

If you want to determine whether a specified key-value pair exists in the dictionary, first determine whether there is a corresponding key in the dictionary. To determine whether a dictionary contains the keys of a specified key-value pair, you can use the in or not in operators.

It should be noted that, for dict, the in or not in operator is judged based on the key.

For example, the following code:

age = {'Jason': 95, 'Nicole': 29, 'Harry': 10}
# Determine if a contains a key named 'Nicole'
print ('Nicole' in age) # True
# Determine if a contains a key named 'Peter'
print ('Peter' in age) # False

The output is:



With the in (or not in) operator, we can easily determine whether a dictionary contains a key. If it exists, since the corresponding value can be easily obtained by the key, it is easy to determine the dictionary. Whether there are specified key-value pairs.


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises