Master Python Dictionaries: Creation, Access, and Manipulation Techniques
This tutorial explains Python's built‑in dictionary type, covering how to create dictionaries with braces or the dict function, access values via keys or get(), and perform update, addition, and deletion operations using methods like update(), pop(), popitem() and clear().
Introduction
Dictionary is a built‑in Python data structure that provides a semantic way to store structured data and is widely used in development.
Dictionary Overview
1. Dictionaries are created with curly braces {} or the dict function and can be nested.
2. They consist of key‑value pairs.
3. Keys and values are separated by a colon :, and pairs are separated by commas ,.
4. Keys must be unique, while values may repeat.
5. Keys should not be Chinese or other non‑ASCII characters, following common conventions.
Creating Dictionaries
Dictionaries can be created using curly braces {} or the dict function.
1. Using curly braces
emp = {'name':'张三', 'age':22, 'sex':'男'}
print(emp)
# {'name': '张三', 'age': 22, 'sex': '男'}
print(type(emp))
# <class 'dict'>2-1. Using dict function
The dict function creates a dictionary without needing quotes around the keys.
emp = dict(name='张三', age=22, sex='男')
print(emp)
# {'name': '张三', 'age': 22, 'sex': '男'}2-2. Using fromkeys method
The fromkeys method together with dict creates a dictionary with keys from a sequence.
emp = dict.fromkeys(['name','age','sex'])
print(emp)
# {'name': None, 'age': None, 'sex': None}If a default value is needed, pass it as the second argument:
emp = dict.fromkeys(['name','age','sex'], 'N/A')
print(emp)
# {'name': 'N/A', 'age': 'N/A', 'sex': 'N/A'}Accessing Dictionary Values
Two ways to retrieve values.
1. Bracket notation
emp = {'name':'张三', 'age':22, 'sex':'男'}
print(emp['name'])
# 张三Note: Accessing a non‑existent key raises KeyError.
2. get method
emp = {'name':'张三', 'age':22, 'sex':'男'}
v = emp.get('name')
print(v) # 张三
v = emp.get('dept', '其他部门')
print(v) # 其他部门Dictionary Write Operations
Update, add, and delete entries.
Update
Python follows “update if exists, otherwise add”.
1. Update a value
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
emp['dept'] = '推广部'
print(emp)
# {'name': '张三', 'age': 22, 'sex': '男', 'dept': '推广部'}2. Bulk update with update()
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
emp.update(age=18, dept='推广部')
print(emp)
# {'name': '张三', 'age': 18, 'sex': '男', 'dept': '推广部'}Add
Adding follows the same “update if exists, otherwise add” rule.
1. Add a new key
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
emp['job'] = '销售'
print(emp)
# {'name': '张三', 'age': 22, 'sex': '男', 'dept': '研发部', 'job': '销售'}2. Bulk add with update()
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
emp.update(dept='推广部', job='推广员')
print(emp)
# {'name': '张三', 'age': 22, 'sex': '男', 'dept': '推广部', 'job': '推广员'}Delete
1. pop() removes a specific key and returns its value
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
dept = emp.pop('dept')
print(emp) # {'name': '张三', 'age': 22, 'sex': '男'}
print(dept) # 研发部2. popitem() removes and returns the last key‑value pair
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
kv = emp.popitem()
print(emp) # {'name': '张三', 'age': 22, 'sex': '男'}
print(kv) # ('dept', '研发部')3. clear() empties the dictionary
emp = {'name':'张三', 'age':22, 'sex':'男', 'dept':'研发部'}
emp.clear()
print(emp) # {}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.
