logo codesdope

Dictionary in Python


Dictionary is the most important data structure of Python. Let's take an example to explain it.
Suppose, the cost of mango is 40
the cost of banana is 10
the cost of cherry is 20
To store them, we can use a list or a tuple, but there is another good way to store all these in a better way.
Like, list is []
tuple is ()
dictionary is {}
Let's code using dictionary.

fruit = {'mango':40,'banana':10,'cherry':20}
Output

The above code means that 'mango' is related to 40, 'banana' to 10 and 'cherry' to 20.

We access the elements of a list 'a' by a[0], a[1] and so on. For dictionary, we do this:

fruit = {'mango':40,'banana':10,'cherry':20}
print fruit['mango']
print fruit['banana']
Output
40
10

Here, 'mango', 'banana', etc are called keys.
And 40, 10 and 20 are values.

d = {}
d['a'] = 1
d['b'] = 2
print d
Output
{'a': 1, 'b': 2}

We can also get all keys and values at one go by using the key() and value() functions respectively. key and value return a list containing all keys and values of a dictionary respectively. Let's see an example of these functions:

fruit = {'mango':40,'banana':10,'cherry':20}
print fruit.keys()
print fruit.values()
Output
['cherry', 'mango', 'banana']
[20, 40, 10]

Let's see one more example of dictionary containing elements of different types.

a = {'x':[1,2,3,4],'y':"Hello World",'z':4.0}
Output

Here, 'x', 'y' and 'z' are called keys. And [1,2,3,4], "Hello World" and 4.0 are their values respectively.

Let's take one more example of sorting ( arranging in ascending order ) the scores of few students

score = {40:"x",30:"y",50:"z",45:"a"}
for s in sorted(score):
	print s,":",score[s]
Output
30 : y
40 : x
45 : a
50 : z

Here, we have stored the scores of different students using dictionary.
for a in sorted(score) - sorted() gives a new list in which all the keys are sorted in ascending order. You can also use sorted function for a list or tuple.
print s,":",score[s] - As s is from sorted(score), so, first score[30] will come, then score[40] and then score[45] and at last score[45].
So, this is the Pythonic way of making your things easy. We just printed all the names with their score in increasing order.

You can also use the sorted() funcion on a list.
We can use reverse function on list to reverse the whole list. If a is your list, then try a.reverse(). sorted(score) gave you [30,40,45,50] and sorted(score).reverse() will give you [50,45,40,30]. Try this on your own.

reverse function in python

Like list, we can also use len() function for dictionary.
One more important ( rather most important ) feature of dictionary is has_key().
has_key() returns True if the given parameter is in key, else it gives False.
Let's try this.

score = {40:"x",30:"y",50:"z",45:"a"}
print score.has_key(40)
print score.has_key(90)
Output
True
Fasle

has_key has been removed in Python 3. You can use in operator instead i.e.,
print(40 in score.keys()).

In the next section, you are going to make your own functions. So, solve questions on dictionary and then go for function.

Don't practice until you get it right. Practice until you can't get it wrong.


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.