Introducing Python © Chirag Wazir 2002 Prev   Index   Next

Dictionaries

Keys can be any immutable type which only contains immutable types

fruits = {'apple': 'red', 'banana': 'yellow'}
fruits['grape'] = 'green'
fruits
      {'grape': 'green', 'apple': 'red', 'banana': 'yellow'}
fruits['banana'] = 'yellow/green'
fruits
      {'grape': 'green', 'apple': 'red', 'banana': 'yellow/green'}
fruits['apple']
      'red'
fruits.keys()
      ['grape', 'apple', 'banana']
fruits.values()
      ['green', 'red', 'yellow/green']
fruits.has_key('orange')
      0
'banana' in fruits
      1
del fruits['banana']
fruits
      {'grape': 'green', 'apple': 'red'}