Mengubah dictionary

Tipe Data di Python

Jason Myers

Instructor

Menambah dan memperluas dictionary

  • Penugasan untuk menambah key/value baru ke dictionary
  • Metode .update() untuk memperbarui dictionary dari dictionary lain, tuple, atau keyword
print(galleries_10007)
{'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
art_galleries['10007'] = galleries_10007
Tipe Data di Python

Memperbarui 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'}
Tipe Data di Python

Menghapus dengan pop dan del pada dictionary

  • Instruksi del menghapus key/value
  • Metode .pop() menghapus key/value dengan aman dari dictionary
del art_galleries['11234']

galleries_10310 = art_galleries.pop('10310')
print(galleries_10310)
{'New Dorp Village Antiques Ltd': '(718) 815-2526'}
Tipe Data di Python

Ayo berlatih!

Tipe Data di Python

Preparing Video For Download...