Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Dictionary Operations

A dict only permits a single operation: []. This is used to add, change or retrieve items from the dict. The slicing operations that apply to sequences don't apply to a dict.

Examples of dict operations.

>>> 
d= { }

>>> 
d[2]= [ (1,1) ]

>>> 
d[3]= [ (1,2), (2,1) ]

>>> 
d

{2: [(1, 1)], 3: [(1, 2), (2, 1)]}
>>> 
d[2]

[(1, 1)]
>>> 
d["2 or 3"]= d[2] + d[3]

>>> 
d["2 or 3"]

[(1, 1), (1, 2), (2, 1)]
            

This example starts by creating an empty dict, d. Into d[2] it inserts a list with a single tuple. Into d[3] it inserts a list with two tuples. When the entire dict is printed it shows the two key:value pairs, one with a key of 2 and another with a key of 3.

Then it creates an entry with a key of the string "2 or 3". The value for this entry is computed from the values of d[2] + d[3]. Since these two entries are lists, the lists can be combined with the + operator. The resulting expression is stored into the dict.

When we print the final dict, we see that there are three key:value pairs: one with a key of 3, one with a key of 2 and one with a key of "2 or 3".

{3: [(1, 2), (2, 1)], 2: [(1, 1)], 
'2 or 3': [(1, 1), (1, 2), (2, 1)]}

This ability to use any object as a key is a powerful feature, and can eliminate some needlessly complex programming that might be done in other languages.

Here are some other examples of picking elements out of a dict.

>>> 
myBoat = { "NAME":"KaDiMa",

... 
    "LOA":18, "SAILS":["main","jib","spinnaker"] }

...

>>> 
myBoat["NAME"]

KaDiMa
>>> 
for s in myBoat["SAILS"]:

... 
    print s

...

main
jib
spinnaker

            

String Formatting with Dictionaries. The string formatting operator, % , can be applied to a dict as well as a sequence. When this operator was introduced in Chapter 12, Strings , the format specifications were applied to a tuple or other sequence. When used with a dict, each format specification is given an additional option that specifies which dict element to use. The general format for each conversion specification is:

%( element )[ flags ][ width [. precision ]] code

The flags , width , precision and code elements are defined in Chapter 12, Strings . The element field must be enclosed in ()'s; this is the key to be selected from the dict.

For example:

print "%(NAME)s, %(LOA)d" % myBoat

This will find myBoat[NAME] and use %s formatting; it will find myBoat[LOA] and use %d number formatting. We'll return to another application of this when we talk about classes in Chapter 21, Classes .


 
 
  Published under the terms of the Open Publication License Design by Interspire