Topics

Dictionary

Introducing Dictionary

Python Dictionary is a container type for storing data in key/value pairs. The data is enclosed within the curly braces {}. It is similar to associative array in php and hashtable and hashmap in Java. A dictionary can be created in the following ways:


# creating a dictionary with values assigned
>>> a = {'a':'apple', 'b':'boy', 'c':'cat'}

# printing its content             
>>> a                                                   
{'a': 'apple', 'b': 'boy', 'c': 'cat'}

# getting its data type
>>> type(a)                                             
<type 'dict'>

# create empty dictionary
>>> b = dict()

# add a new key/value pair to the dictionary                                         
>>> b['john'] = 10

# add another key/value pair to the dictionary                                      
>>> b['mary'] = 20                                      
>>> b
{'john': 10, 'mary': 20}

# change the value of existing key: 'john'
>>> b['john'] = 30                                      
>>> b
{'john': 30, 'mary': 20}

>>> c = [('a', 1), ('b', 2), ('c', 3)]
# create a dictionary from a list of tuples.
>>> dict(c)                                            
{'a':1, 'b':2, 'c':3}           
						

Accessing Values in Dictionary

There are a few built-in methods for accessing all the keys, values or individual elements in a dictionary.


>>> x = {'a':'apple', 'b':'boy', 'c':'cat'}

# getting the value for the key: 'a'
>>> x['a']                                          
'apple'

# another way of getting the value for the key: 'a'
>>> x.get('a')   

# Check whether the key: 'a' exists                                   
>>> x.has_key('a')                                  
True

# key not found, produces error
>>> x['d']                                          
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
KeyError: 'd'

# get all the keys in the dictionary
>>> x.keys()                                        
['a', 'c', 'b']

# get all the values in the dictionary
>>> x.values()                                      
['apple', 'cat', 'boy']

# get all the key/value pairs in the dictionary
>>> x.items()                                       
[('a', 'apple'), ('c', 'cat'), ('b', 'boy')]      
						

Deleting Dictionary and its Contents

See examples below on how to remove one element or all the elements in a dictionary.


>>> x = {'a':'apple', 'b':'boy', 'c':'cat'}

# delete the element with key: 'a'
>>> del x['a']                                      
>>> x
{'b': 'boy', 'c': 'cat'}

# empty the dictionary
>>> x.clear()                                       
>>> x
{}

# delete the dictionary
>>> del x   

# dictionary no longer exists, produces error                                        
>>> x                                               
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'x' is not defined
						
2014 KILLER BEAN

XtGem Forum catalog