<Name of the Dictionary >= {<key1> : <Value1> , <key2> : <Value2>,...., <key n> : <Value n> }
>>> COURSE ={'ORACLE': 1 ,'LINUX' : 2, 'SHELL' : 3 ,'PERL' : 4 }
>>> type(COURSE)
<type 'dict'>
>>> COURSE
{'ORACLE': 1, 'PERL': 4, 'SHELL': 3, 'LINUX': 2}
This will display the key and values within the dictionary, this are not ordered and if we want the dictionary to be in order then we have to sort it by values as the each key can be unique.
>>> COURSE ['PERL']
4
>>> COURSE ['PYTHON'] = 4
>>> COURSE
{'ORACLE': 1, 'PERL': 4, 'PYTHON': 4, 'SHELL': 3, 'LINUX': 2}
>>>
>>> COURSE ['PYTHON'] = 9
>>> COURSE
{'ORACLE': 1, 'PERL': 4, 'PYTHON': 9, 'SHELL': 3, 'LINUX': 2}
>>>
If we want to display the keys and values of a dictionary then we have to execute the name of the dictionary with key or values as like below:
>>> COURSE.keys()
['ORACLE', 'PERL', 'PYTHON', 'SHELL', 'LINUX']
>>> COURSE.values()
[1, 4, 9, 3, 2]
>>> del COURSE['PERL']
>>> COURSE
{'ORACLE': 1, 'PYTHON': 9, 'SHELL': 3, 'LINUX': 2}
>>>
Now we can see that the element PERL is deleted from the dictionary.
>>> for key,val in COURSE.iteritems():
print key,val
ORACLE 1
PYTHON 9
SHELL 3
LINUX 2
>>>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (7)
Commented:
The order might be a result of the version and flavor of your Pyton interpreter.
It is wise to double-check what is rendered. I've had content/tags disappear when I saved an article in multiple editing sessions.
=========
spelling error: "delimeted" => "delimited"
=========
I was referring to the phrase "key value" and suggested that some delimiter be used in your description.
Author
Commented:Commented:
You should also follow the "PEP 8 -- Style Guide for Python Code" http://legacy.python.org/dev/peps/pep-0008/
Commented:
There's a lot of version 2 Python development still being done. Some packages, such as Portable Python, are version 2.
Commented:
There is also a lot of Fortran and Cobol code around.
View More