"Python Programming" 010 – Python Dictionary: A powerful tool for efficient storage and manipulation of key-value pairs

What is a dictionary?

Dictionary is an important data structure in Python, which can store and operate key-value pairs. A dictionary is an unordered collection in which each element consists of a key and a corresponding value. Dictionary keys must be unique, while values ​​can be any type of data. Dictionaries are also known as associative arrays, maps, or hash tables in other programming languages.

Create dictionary

In Python, you can create a dictionary using curly braces ({}) or the dict() function. Here is an example of creating a dictionary:

# 使用花括号创建字典

my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}

# 使用dict()函数创建字典

my_dict = dict(key1="value1", key2="value2", key3="value3")

Access and modify dictionaries

Values ​​in a dictionary can be accessed using keys. To access the value corresponding to a specific key, you can use the following syntax:

value = my_dict["key1"]

If the key does not exist, an errorKeyError will be raised. To avoid raising an error, you can use the get() method to access the value and provide a default value:

value = my_dict.get("nonexistent_key", "default_value")

To modify the value corresponding to a specific key in the dictionary, you can use the following syntax:

my_dict["key1"] = "new_value1"

If the key does not exist, this will create a new key-value pair in the dictionary.

Add and remove dictionary items

To add a new key-value pair to the dictionary, you can use an assignment statement:

my_dict["new_key"] = "new_value"

To delete key-value pairs in the dictionary, you can usedelkeyword:

del my_dict["key1"]

In addition, you can also use thepop() method to delete the key-value pair of the specified key and return its value:

value = my_dict.pop("key1")

Common methods of dictionary

Python provides many built-in methods for manipulating dictionaries. The following is an introduction to some commonly used methods:

  • keys(): Returns a list containing all keys of the dictionary.
  • values(): Returns a list containing all values ​​of the dictionary.
  • items(): Returns a list containing all key-value pairs of the dictionary, each key-value pair is represented as a tuple.
  • get(key, default): Returns the value of the specified key, or the default value if the key does not exist.
  • update(other_dict): Update key-value pairs in another dictionary to the current dictionary.
  • clear(): Remove all key-value pairs from the dictionary.

Dictionary application scenarios

Dictionaries have a wide range of application scenarios in Python programming. They can be used to store and access configuration information, process JSON data, build mapping relationships, cache data, etc. The flexibility and efficiency of dictionaries make them ideal for solving a variety of problems.

Summarize

This article introduces the basic concepts of dictionaries in Python, creating and accessing dictionaries, adding and deleting dictionary items, and commonly used dictionary methods. Dictionary is an important data structure that plays an important role in Python programming. By using dictionaries flexibly, data can be processed and organized more efficiently.

Guess you like

Origin blog.csdn.net/zclmoon/article/details/132150243