<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Dictionaries in Python

Published on
4,671 Points
1,571 Views
1 Endorsement
Last Modified:
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
Comment
Author:Swadhin Ray
  • 3
  • 2
  • 2
7 Comments
LVL 48

Expert Comment

by:aikimark
Some feedback comments:

Syntax:
<>  ={<> : <> , <> : <>, …<> : <>  }
 
From the above syntax the value can be integer may not be enclosed by single/double quotes and separated by commas.
1. It would be more helpful if you had put something more meaningful between the <> characters.  In the following example, the <> are replaced by a code snippet that better conveys more options.
Example:
dictVariableName = { 'key1' : 'some string value' , 'key2' : "another string value", "key3" : numericvalue  }

Open in new window

2. "From the above syntax" the above syntax does not indicate the types of values that can be associated with the dictionary keys.

this are not ordered
3. there appears to be some ordering that does take place.

key value pairs
4. this is a minor complaint, but there is usually some delimiter between "key" and "value".  While not very important for experienced developers, people new to programming might not understand this.  Without punctuation, this might be interpreted as some values are key (vitally important) to the working of the dictionary.
Suggestions:
key:value pairs
key-value pairs
key/value pairs
(key , value) pairs (or tuples)
key=value pairs
key~value pairs
0
LVL 17

Author Comment

by:Swadhin Ray
Some how I didn't noticed that the syntax was removed. If i keep it within the greater than and less than symbol was not taking in, so made it on codes.
On point no 4 was already mentioned that that should be separated by a comma.
On point no 3 what I meant was not on order the way it was defined, if you see it picks randomly.
0
LVL 48

Expert Comment

by:aikimark
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.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

LVL 17

Author Comment

by:Swadhin Ray
Thanks for pointing that out :) .. Updated article.
0
LVL 30

Expert Comment

by:pepr
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/
0
LVL 48

Expert Comment

by:aikimark
@pepr

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

Expert Comment

by:pepr
@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.
0

Featured Post

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Join & Write a Comment

Suggested Articles

Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month