删除键值对

  1. alien_0 = {'color': 'green', 'points': 5}
  2. print(alien_0)
  3. del alien_0['points']
  4. print(alien_0)

3.遍历字典

  1. user_0 = {
  2. 'username': 'efermi',
  3. 'first': 'enrico',
  4. 'last': 'fermi',
  5. }
  6. for key, value in user_0.items():
  7. print("\nKey: " + key)
  8. print("Value: " + value)

遍历字典中的所有键

  1. favorite_languages = {
  2. 'jen': 'python',
  3. 'sarah': 'c',
  4. 'edward': 'ruby',
  5. 'phil': 'python',
  6. }
  7. for name in favorite_languages.keys():
  8. print(name.title())