Python dict() function
dict()It is a built-in function in Python used to create dictionaries (dictionary).
A dictionary is a key-value data structure in Python, where each key corresponds to a value.dict()The function can create dictionaries in multiple ways.
Word definition: dictYesdictionaryAbbreviation of "dictionary".
Basic syntax and parameters
Syntax format
dict() dict(mapping) dict(**kwargs) dict(iterable, **kwargs)
Parameter description
- Parameter mapping:
- Type: Mapping object (such as a dictionary)
- Description: Create a new dictionary from a mapping object.
- Parameter kwargs:
- Type: Keyword arguments
- Description: Use keyword arguments to create a dictionary.
- Parameter iterable:
- Type: Iterable object
- Description: An iterable whose elements are key-value pairs.
Function description
- Return value: Returns a dictionary object.
- Features: The keys of a dictionary must be immutable types (such as strings, numbers, tuples).
Examples
Example 1: Creating a dictionary
Example
# Empty dictionary
d = dict()
print(d) # Output: {}
# Keyword arguments (most commonly used)
d = dict(name="Tom", age=20)
print(d) # Output: {'name': 'Tom', 'age': 20}
# Create from a mapping object
d1 = {"name": "Tom"}
d2 = dict(d1)
print(d2) # Output: {'name': 'Tom'}
# Create from an iterable (elements are key-value pairs)
pairs = [("name", "Tom"), ("age", 20)]
d = dict(pairs)
print(d) # Output: {'name': 'Tom', 'age': 20}
# Create from zip
keys = ["name", "age", "city"]
values = ["Tom", 20, "Beijing"]
d = dict(zip(keys, values))
print(d) # Output: {'name': 'Tom', 'age': 20, 'city': 'Beijing'}
d = dict()
print(d) # Output: {}
# Keyword arguments (most commonly used)
d = dict(name="Tom", age=20)
print(d) # Output: {'name': 'Tom', 'age': 20}
# Create from a mapping object
d1 = {"name": "Tom"}
d2 = dict(d1)
print(d2) # Output: {'name': 'Tom'}
# Create from an iterable (elements are key-value pairs)
pairs = [("name", "Tom"), ("age", 20)]
d = dict(pairs)
print(d) # Output: {'name': 'Tom', 'age': 20}
# Create from zip
keys = ["name", "age", "city"]
values = ["Tom", 20, "Beijing"]
d = dict(zip(keys, values))
print(d) # Output: {'name': 'Tom', 'age': 20, 'city': 'Beijing'}
Expected output:
{}
{'name': 'Tom', 'age': 20}
{'name': 'Tom'}
{'name': 'Tom', 'age': 20}
{'name': 'Tom', 'age': 20, 'city': 'Beijing'}
Code analysis:
- Keyword arguments are the most common way to create a dictionary.
- The elements of an iterable must be key-value pairs (tuples or lists).
dict(zip())It is a common technique to merge two lists into a dictionary.
Example 2: Dictionary operations
Example
# Access value
d = {"name": "Tom", "age": 20}
print(d["name"]) # Output: Tom
print(d.get("age")) # Output: 20
# Add/Modify
d["city"] = "Beijing"
d["age"] = 21
print(d) # Output: {'name': 'Tom', 'age': 21, 'city': 'Beijing'}
# Delete
del d["age"]
print(d) # Output: {'name': 'Tom', 'city': 'Beijing'}
# Iterate
for key, value in d.items():
print(f"{key}: {value}")
d = {"name": "Tom", "age": 20}
print(d["name"]) # Output: Tom
print(d.get("age")) # Output: 20
# Add/Modify
d["city"] = "Beijing"
d["age"] = 21
print(d) # Output: {'name': 'Tom', 'age': 21, 'city': 'Beijing'}
# Delete
del d["age"]
print(d) # Output: {'name': 'Tom', 'city': 'Beijing'}
# Iterate
for key, value in d.items():
print(f"{key}: {value}")
Expected output:
Tom
20
{'name': 'Tom', 'age': 21, 'city': 'Beijing'}
{'name': 'Tom', 'city': 'Beijing'}
name: Tom
city: Beijing
Dictionaries support rich operations: access, add, modify, delete, iterate, etc.
Other extensions
Python3 built-in functions