Advertisement
wagner-cipriano

working with dict

Oct 15th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. #python working with dict
  2. from __future__ import print_function      #Compatibilidade func print python 2/3
  3.  
  4. #python dict
  5. d = {'id':1, 'name':'nome', 'desc':'descricao'}
  6.  
  7. #Print keys and values of dict
  8. for key in d.keys():
  9.     print (key, d[key])
  10.  
  11. #Print keys and values of dict using items funcion
  12. #  and formating string position
  13. for key, val in d.items():
  14.     print (key.ljust(6), val)
  15.  
  16. #check if dict has an key
  17. d.has_key('key')  #only python 2
  18. 'key' in d.keys()   #python 2,3
  19.  
  20. #Remove key id
  21. d.pop('desc')
  22.  
  23. #update dict
  24. new_dict = {'name':'Jose'}
  25. d.update(new_dict)  #d = {'desc': 'descricao', 'id': 1, 'name': 'Jose'}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement