Altering dictionaries

Data Types in Python

Jason Myers

Instructor

Adding and extending dictionaries

  • Assignment to add a new key/value to a dictionary
  • .update() method to update a dictionary from another dictionary, tuples or keywords
print(galleries_10007)
{'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
art_galleries['10007'] = galleries_10007
Data Types in Python

Updating a 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'}
Data Types in Python

Popping and deleting from dictionaries

  • del instruction deletes a key/value
  • .pop() method safely removes a key/value from a dictionary.
del art_galleries['11234']

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

Let's practice!

Data Types in Python

Preparing Video For Download...