Categories: S60 | Python | Code Examples
This page was last modified 18:11, 15 July 2008.
Dictionary in Python
From Forum Nokia Wiki
Contents |
Introduction
Dictionary is an object in Python which is used to save standard or rather unique key-value pairs. Dictionaries are important concept in python and play important role at many occasions.
Creating a Dictionary
To create a Dictionary look at the following.
d = {"country": "India", "Place": "Bhavnagar", "Occupation":"student"} e = {}
we can observe that in the above code sample a dictionary is defined in the first case variable d is the dictionary initialized with three key-value pairs. We can have as many key value pairs we want. In the first case Country is the key and another string following that after the operator ":" is the value of that key here in this case its India. Similarly the other cases can be explained. Instead of Strings we can have the numbers also. The second variable "e" initializes with an empty dictionary. Values against the keys can be any type of objects like a image or an file object.
Working With Dictionary
Accessing the dictionary logically according to our own need is the requirement. A dictionary can be accessed or its key-value pair can be modified in many ways. The code example following will explain it in a good manner. Lets consider the above defined dictionary for the purpose of understanding.
print d["country"] print d["place"] d["place"] = "Jamnagar" print d["place"]
The above code example seems very easy. In the first statement the output will be the value of the key "country" i.e. "India". Same the case is in the second statement. In the third statement we are modifying the value to the key. So the fourth statement will print "Jamnagar" as we have changed the value of the "place" key from "Bhavnagar" to "Jamnagar". In a dictionary one can change values and add new key-value pairs as such required. However one thing to notice is that a key can be mapped with only one value. If one wants to have a multiple values per key then using a list is a suitable option.
Deleting
Deleting something in a dictionary is very easy have a look at the code below:
del d["occupation"]
this will delete the key-value pair in the dictionary initialized.
Decision Making and Looping
using the IF statement we can test if a key exist in a dictionary or not.
import appuifw d = {"country": "India", "Place": "Bhavnagar", "Occupation":"student"} if "place" in d: appuifw.note(u"place exits","info") else: appuifw.note(u"there is no place","info")
Now using the for loop we can access the all the values in the dictionary. We can get the number of key value pairs by using the function len(d).
for key, value in d.items{} print "Key",key, "Value",value
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Get creative with python | bercobeute | Python | 0 | 2005-12-14 15:24 |
| Ask The Expert: dcrocha on Python | Nokia Ron | Interviews & Ask the Experts | 15 | 2008-04-21 20:51 |
| Python integration with NetBeans | girdhar | Python | 11 | 2008-04-10 00:58 |
| How to edit Python files on the phone itself? | tristanr | Python | 6 | 2005-11-22 01:38 |
| Doubt | rishabhgupta | Python | 3 | 2008-06-17 13:48 |
