Bài 26. Dữ liệu kiểu dictionary
Khởi tạo dict
d = {}
d = {1: 'apple', 2: 'ball'}
d = {'name': 'John', 1: [2, 4, 3]}
d = dict({1:'apple', 2:'ball'})
d = dict([(1,'apple'), (2,'ball')])Truy cập phần tử dict
d = {'name': 'Jack', 'age': 26}
print(d['name']) # Jack
print(d.get('age')) # 26
print(d.get('address')) # None (truy cập phần tử không tồn tại)
print(d['address']) # Báo lỗi KeyErrorThêm, sửa, xóa phần tử dict
Thêm, sửa
Xóa
Các phương thức với dict
Dictionary Comprehension
Hàm dựng sẵn cho dict
Thao tác khác
Kiểm tra key chứa trong dict
Lặp theo phần tử dict
Last updated