cfabio

Dictionary.py

Jan 5th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. dict ={'size' : 1, 'value' : 'bla'}
  2.  
  3. #opposite as list they are not ordered!!
  4. dict2 ={'value' : 'bla', 'size' : 1}
  5. dict1 == dict2 #evaluates as True
  6.  
  7. #they are mutable data
  8.  
  9. 'value' in dict
  10. 'value' not in dict
  11.  
  12. list(dict.keys())
  13. list(dict.values())
  14. list(dict.items())
  15.  
  16. #iterations
  17. for k in dict.keys() :
  18. for k in dict.values() :
  19. for k, v in dict.items() :
  20. #print a set of tuple. Same as list but no square brackets, and they are immutable
  21. for i in dict.items() :  
  22.  
  23.  
  24. if 'size' in dict
  25.     print(dict['size'])
  26. dict.get('size', 0)   #get the value, default if it doesn't exist
  27.  
  28. #to be sure the key is existing, before we assign a value to it
  29. dict.setdefault('char', 0); #if the key is not existing add it and assign value 0, return the value otherwise
  30.  
  31. dict[char] = 1;
  32.  
  33. ##########################
  34. Data structure
  35.  
  36. myList = [{'a' : 1, 'b' : 2}, {'c' : 1, 'd' : 2}] #list of dictionaries
Advertisement
Add Comment
Please, Sign In to add comment