The keys
method returns the list of keys.
The value
method returns the list of values.
The items
methods returns the list of 2-tuples of key-value pairs.
>>> h = {"john": 42.0, "mary": 999, 6.046: 'A'} >>> h.keys() ['john', 'mary', 6.0460000000000003] >>> h.values() [42.0, 999, 'A'] >>> h.items() [('john', 42.0), ('mary', 999), (6.0460000000000003, 'A')]
The has_key
method returns True
if the given key is in the dictionary.
>>> h.has_key("john") True >>> h.has_key("judith") False >>> h.has_key(6.046) True >>> h.has_key(6.043) False
The clear
method removes all the items from the dictionary.
>>> h.clear() >>> h {}
The copy
method creates a new dictionary with the same key and value pairs, but the dictionary is not the same as the original one. So, change done to one will not affect the other.
>>> h = {"alyssa":42, "ben":4649, "clark":44.193} >>> h.copy() {'ben': 4649, 'alyssa': 42, 'clark': 44.192999999999998} >>> g = h.copy() >>> h["alyssa"] = 999 >>> h {'ben': 4649, 'alyssa': 999, 'clark': 44.192999999999998} >>> g {'ben': 4649, 'alyssa': 42, 'clark': 44.192999999999998} >>> g["alyssa"] = 765 >>> g {'ben': 4649, 'alyssa': 765, 'clark': 44.192999999999998}