Understanding and Using Python Dictionaries: Creation, Access, Modification, Deletion, Traversal, and Nesting
This tutorial explains Python dictionaries in depth, covering their definition, how to create, access, modify, add, delete, copy, traverse, and nest dictionary entries, with clear code examples for each operation to help beginners master this essential data structure.
1. Dictionary Understanding
Python dictionaries are mutable containers that store key‑value pairs, where each pair is separated by a colon and the whole structure is enclosed in curly braces.
2. Accessing Dictionaries
2.1 Access Keys
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
keys = my_dict.keys()
print(keys) # 输出: dict_keys(['name', 'age', 'city'])2.2 Access Values
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
values = my_dict.values()
print(values) # 输出: dict_values(['Alice', 30, 'New York'])3. Creating Dictionaries and Accessing Values
3.1 Create and Access
# 创建字典
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# 访问字典中的值
print(my_dict['name']) # 输出: Alice
print(my_dict.get('age')) # 输出: 303.2 Dictionary Length
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(len(my_dict)) # 输出: 33.3 Data Types
my_dict = {
'name': 'Alice', # 字符串
'age': 30, # 整数
'is_student': False, # 布尔值
'grades': [95, 88, 92] # 列表
}
print(my_dict) # 输出: {'name': 'Alice', 'age': 30, 'is_student': False, 'grades': [95, 88, 92]}4. Modifying Dictionaries
4.1 Single Change
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
my_dict['age'] = 31
print(my_dict) # 输出: {'name': 'Alice', 'age': 31, 'city': 'New York'}4.2 Multiple Changes
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
my_dict.update({'age': 31, 'city': 'Los Angeles'})
print(my_dict) # 输出: {'name': 'Alice', 'age': 31, 'city': 'Los Angeles'}5. Adding Dictionary Items
5.1 Direct Assignment
my_dict = {'name': 'Alice', 'age': 30}
my_dict['city'] = 'New York'
print(my_dict) # 输出: {'name': 'Alice', 'age': 30, 'city': 'New York'}5.2 Using update()
my_dict = {'name': 'Alice', 'age': 30}
my_dict.update({'city': 'New York', 'email': '[email protected]'})
print(my_dict) # 输出: {'name': 'Alice', 'age': 30, 'city': 'New York', 'email': '[email protected]'}6. Deleting Dictionary Items
6.1 Delete Specific Key
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
del my_dict['age']
print(my_dict) # 输出: {'name': 'Alice', 'city': 'New York'}6.2 Using pop()
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
removed_value = my_dict.pop('age')
print(removed_value) # 输出: 30
print(my_dict) # 输出: {'name': 'Alice', 'city': 'New York'}6.3 Clear Dictionary
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
my_dict.clear()
print(my_dict) # 输出: {}7. Traversing Dictionaries
7.1 Iterate Keys
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
for key in my_dict:
print(key)7.2 Iterate Values
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
for value in my_dict.values():
print(value)7.3 Iterate Key‑Value Pairs
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
for key, value in my_dict.items():
print(f"{key}: {value}")8. Copying Dictionaries
8.1 Using copy() Method
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
new_dict = my_dict.copy()
print(new_dict) # 输出: {'name': 'Alice', 'age': 30, 'city': 'New York'}8.2 Using dict Constructor
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
new_dict = dict(my_dict)
print(new_dict) # 输出: {'name': 'Alice', 'age': 30, 'city': 'New York'}9. Nested Dictionaries
9.1 Create Nested Dictionary
nested_dict = {
'person1': {'name': 'Alice', 'age': 30},
'person2': {'name': 'Bob', 'age': 25}
}
print(nested_dict) # 输出: {'person1': {'name': 'Alice', 'age': 30}, 'person2': {'name': 'Bob', 'age': 25}}9.2 Access Nested Dictionary
nested_dict = {
'person1': {'name': 'Alice', 'age': 30},
'person2': {'name': 'Bob', 'age': 25}
}
print(nested_dict['person1']['name']) # 输出: Alice9.3 Modify Nested Dictionary
nested_dict = {
'person1': {'name': 'Alice', 'age': 30},
'person2': {'name': 'Bob', 'age': 25}
}
nested_dict['person1']['age'] = 31
print(nested_dict) # 输出: {'person1': {'name': 'Alice', 'age': 31}, 'person2': {'name': 'Bob', 'age': 25}}Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.