Dictionaries in Python

Swadhin Ray
CERTIFIED EXPERT
Published:
Updated:
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can define dictionaries using dictionary construct.
First of all we need to define a dictionary:
Syntax:

<Name of the Dictionary >= {<key1> : <Value1> , <key2> : <Value2>,...., <key n> : <Value n> }

Open in new window


 
From the above syntax the value can be integer may not be enclosed by single/double quotes and separated by commas.
Example:
 
>>> COURSE ={'ORACLE': 1 ,'LINUX' : 2, 'SHELL' : 3 ,'PERL' : 4 }
>>> type(COURSE)
<type 'dict'>

Open in new window


The dictionaries are also case sensitive. We can access the above dictionary as like below:
 
>>> COURSE
{'ORACLE': 1, 'PERL': 4, 'SHELL': 3, 'LINUX': 2}

Open in new window

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.
If we want to display the value specifically then we can call the dictionary by each key as like below:
 
>>> COURSE ['PERL']
4

Open in new window


Now if we want to add another element let say ‘PYTHON’ as value 4 then use as below:
 
>>> COURSE ['PYTHON']  = 4
>>> COURSE
{'ORACLE': 1, 'PERL': 4, 'PYTHON': 4, 'SHELL': 3, 'LINUX': 2}
>>> 

Open in new window

 
Now if I want to change the value of the key Linux then we have to use as like below:
>>> COURSE ['PYTHON']  = 9
>>> COURSE
{'ORACLE': 1, 'PERL': 4, 'PYTHON': 9, 'SHELL': 3, 'LINUX': 2}
>>> 

Open in new window

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]

Open in new window


For deleting the key value pair from the dictionary created then we have to use :
 
>>> del COURSE['PERL']
>>> COURSE
{'ORACLE': 1, 'PYTHON': 9, 'SHELL': 3, 'LINUX': 2}
>>> 
 

Open in new window

Now we can see that the element PERL is deleted from the dictionary.
Let us see how we can loop the dictionary values:
 
>>> for key,val in COURSE.iteritems():
                print key,val
 
               
ORACLE 1
PYTHON 9
SHELL 3
LINUX 2
>>> 

Open in new window


 For more information on dictionaries please visit :
https://www.python.org/doc/
1
2,032 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (7)

aikimarkGet vaccinated; Social distance; Wear a mask
CERTIFIED EXPERT
Top Expert 2014

Commented:
I see that you've put the syntax example in a code snippet.  That is the proper way to handle this.  Otherwise, you will need to use the HTML encoding ($gt;) format.  That bad behavior might be an outstanding problem that has been reported.  This is a new editor for articles and they are still getting the bugs out of it.

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.
CERTIFIED EXPERT

Author

Commented:
Thanks for pointing that out :) .. Updated article.
CERTIFIED EXPERT

Commented:
In my opinion, Python 3 should be the prefered one these days. There are reasons -- at least for tutorials.

You should also follow the "PEP 8 -- Style Guide for Python Code" http://legacy.python.org/dev/peps/pep-0008/
aikimarkGet vaccinated; Social distance; Wear a mask
CERTIFIED EXPERT
Top Expert 2014

Commented:
@pepr

There's a lot of version 2 Python development still being done.  Some packages, such as Portable Python, are version 2.
CERTIFIED EXPERT

Commented:
@aikimark: This was my opinion. Beginners should start with the state-of-the-art version of the language, in my opinion. Some operations used in the article are Python 2 specific, and they would look differently in Python 3.

There is also a lot of Fortran and Cobol code around.

View More

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.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community