Pythonically using dictionaries

Data Types in Python

Jason Myers

Instructor

Working with dictionaries more pythonically

  • .items() method returns an object we can iterate over
for gallery, phone_num in art_galleries.items():
    print(gallery)
    print(phone_num)
'Miakey Art Gallery'
'(718) 686-0788'
'Morning Star Gallery Ltd'
'(212) 334-9330'}
'New York Art Expo Inc'
'(212) 363-8280'
Data Types in Python

Checking dictionaries for data

  • .get() does a lot of work to check for a key
  • in operator is much more efficient and clearer
'11234' in art_galleries
False
if '10010' in art_galleries:
    print('I found: %s' % art_galleries['10010'])
else:
    print('No galleries found.')
I found: {'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
Data Types in Python

Let's practice!

Data Types in Python

Preparing Video For Download...