Thay đổi dictionary

Các kiểu dữ liệu trong Python

Jason Myers

Instructor

Thêm và mở rộng dictionary

  • Gán để thêm key/value mới vào dictionary
  • Phương thức .update() cập nhật dictionary từ dictionary khác, tuple hoặc keyword
print(galleries_10007)
{'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
art_galleries['10007'] = galleries_10007
Các kiểu dữ liệu trong Python

Cập nhật dictionary

galleries_11234 = [
  ('A J ARTS LTD', '(718) 763-5473'),
  ('Doug Meyer Fine Art', '(718) 375-8006'),
  ('Portrait Gallery', '(718) 377-8762')]

art_galleries['11234'].update(galleries_11234)
print(art_galleries['11234'])
{'Portrait Gallery': '(718) 377-8762', 
'A J ARTS LTD': '(718) 763-5473', 
'Doug Meyer Fine Art': '(718) 375-8006'}
Các kiểu dữ liệu trong Python

Pop và delete trong dictionary

  • Lệnh del xóa một key/value
  • Phương thức .pop() loại bỏ an toàn một key/value khỏi dictionary
del art_galleries['11234']

galleries_10310 = art_galleries.pop('10310')
print(galleries_10310)
{'New Dorp Village Antiques Ltd': '(718) 815-2526'}
Các kiểu dữ liệu trong Python

Hãy luyện tập!

Các kiểu dữ liệu trong Python

Preparing Video For Download...