Introduction

Hi guyz today I am going to writing a tutorial on dictionaries in Python....As per as getting comments from my readers I'll try to make this tutorial short....

Layout

1.What are dictionaries?
2.Why dictionaries?
3.How to declare/make dictionaries in Python?
4.What all you can do with a dictionary?
5.Interesting Functions?
6.Dictionary Methods?

What are dictionaries?

Dictionaries are simply another type of Sequence in Python..
Like in English dictionaries you search for a word and its meaning … Same happens in python...
Dictionaries in Python have a “key” and a “value of that key”... That may seem strange for the first time but I promise I'll make it easy in the next few topics...

Why Dictionaries?

A common question which comes into ones mind is that why we dictionaries in python..
The answer is pretty simply “When indexing and slicing doesn't do” ….If you are not familiar with that I suggest you to read my previous tutorials on this topic...

Now , That we have a simple picture of these dictionaries ...Let's know how to use them...

How to: Dictionaries in Python?

Declaring dictionaries in python is pretty damn! Simple... I found them easier than any of other sequences I have learn t about..

To declare a dictionary in python We enclose them in curly brasses “{“ and “}”. And rest I'll explain after showing you this piece of code....

dic = { "Aneesh" : 15, "Sonal" : 40 } 
print dic

Output:-

{'Aneesh': 15, 'Sonal': 40}

Yupi , we just declared a dictionary....That wasn't difficult...Was that...

Explanation:

As I told you above that a Dictionaries in Python have a “key” and a “value of that key” Here above we declared 2 keys and two values...

1.Aneesh with a value of 15
2.Sonal with a value of 40

In python we separate a key and its value by a colon [“:”]...

That's pretty simple....

What you can do With a dictionary?

To demonstrate what we can do with a simple dictionary...Lets build a interesting code...

dic = { "Aneesh" : "Programmer" , "Sonal" : "Teacher" , "Sachin" : "Buisnessman" }

print dic["Aneesh"] # It'll print out this keys value...In this case                Programmer.... Basic syntax :- d[k]

dic["Aneesh"] = "Writter" # Assigns the value "Writter to the key "Aneesh"                  in the dictionary "dict" .....Basic                     syntax:- dictionary[key] = value 

String Formatting with Dictionaries:

For this you should know what is string formatting in Python.

dic = { "Aneesh" : "Programmer" , "Sonal" : "Teacher" , "Sachin" : "Buisnessman" } 

print "Hey do you know Aneesh is a %(Aneesh)s and Sonal is a %(Sonal)s " % dic 

Output:

Hey do you know Aneesh is a Programmer and Sonal is a Teacher

Explanation:

Pay close attention to the last line of the Code... … when we write “%(Aneesh)s” % dic the code says to that Search for this key in this dictionary and print it out to me....We use a 's' at the end because we want Python to know that its a String...

INTERESTING FUNCTIONS:

The dict function:

We can use this dict function to construct a dictionary from other mappings eg:- other dictionaries or from sequences of key,value pairs...

tup = ( ('key','value') , ('key2',

Recommended Answers

All 6 Replies

You have code instead of code tag in String Formatting with Dictionaries

Thanks my friend...

Hey guyz please post comments and let me know how's the tutorial....
Current Activity : Waiting for your comments

Sorry guyz I dont know what happened .... My tutorial has only half copied ........

Sorry...I'll contact the admin and edit my tutorial

Can we define the list of dictionary dynamically in for loop? I want to set key and value both dynamically.

''' dict_dynamic101.py
create a dictionary dynamically
tested with Python27 and Python34
'''

# ----------------------------------------------
# add these few lines near the start of you code
import sys
# make string input work with Python2 or Python3
if sys.version_info[0] < 3:
    input = raw_input
# ----------------------------------------------


mydict = {}

while True:
    key = input("Enter the key (quit to exit): ")
    if key == 'quit':
        break
    val = input("Enter the value of the key  : ")
    mydict[key] = val

print(mydict)

''' possible result ...
Enter the key (quit to exit): Jim
Enter the value of the key  : 25
Enter the key (quit to exit): Bob
Enter the value of the key  : 19
Enter the key (quit to exit): Lisa
Enter the value of the key  : 32
Enter the key (quit to exit): quit
{'Jim': '25', 'Bob': '19', 'Lisa': '32'}
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.