Dict
maps hashable values to arbitrary objects
mutable
More than one entry per key not allowed
Keys must be immutable, like strings, numbers, or tuples
Initialize a dict
- # Create a dict
- dict = {}
-
- # Create a dict with initial values
- dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
-
Update
- # Change the value of a key word
- dict['Name'] = 'Lin'
-
- # Add a new key and value
- dict['Major'] = 'CS'
-
Delete
- # Delete an element of the dict
- del dict['Name']
-
- # Remove all elements of the dict
- dict.clear()
-
- # Remove the whole dict
- del dict
-
Build-in functions
- # cmp, compare two dicts
- dict1 = {'Name':'Lin', 'Age':37}
- dict2 = {'Name':'Lin', 'Age':37}
- print cmp(dict1, dict2)
-
- #len, get the number of items in dict
- print len(dict1)
-
- #str, convert a dict to a string
- print str(dict1)
-
- #type, check type
- print type(dict1)
-
- #id, get the memory id of the dict
- print id(dict1)
-
Methods
- #!/usr/bin/python
-
- d = {}; #create a dict
-
- d['Name'] = 'Lin';
- d['Age'] = 38;
-
- #has_key()
- if d.has_key('Name'):
- print 'Has key \'Name\'';
- if 'Name' in d:
- print 'Has key \'Name\'';
-
- #get, returns a value for the given key; if key is not available then returns default value
- print d.get('Name');
- print d.get('Temp', 'Unknown');
- #print d['Temp']; # throw KeyError
-
- #items, return a list of tuples of (key, value) pairs
- items = d.items();
- print items
-
- #keys, return a list of keys
- keys = d.keys();
- print keys;
-
- #values, return a list of values
- values = d.values();
- print values
-
- #setdefault, get the value of key; if key is not available then return default value
- print d.setdefault('Age', None); #38
- print d.setdefault('Sex', None); #None
-
- #update, add a dict to another dict
- d2 = {'Sex': 'M', 'State': 'VA'};
- d.update(d2); #{'Name':'Lin', 'Age':38, 'Sex':'M', 'State':'VA'}
- print d
-
- #remove and retun an arbitrary key-value pair
- print d.popitem();
-
- #!/usr/bin/python
-
- d = {'Name': 'Lin', 'Age': 38, 'Sex': 'M', 'State': 'VA'};
-
- #iterkeys
- i = d.iterkeys();
-
- while 1:
- try:
- e = i.next();
- except StopIteration:
- break;
- else:
- print e,
- print
-
- #itervalues
- iv = d.itervalues();
- for v in iv:
- print v,
- print
-
- #iteritems
- it = d.iteritems();
- for t in it:
- print t;
-
Resources
Python How to Program
STL