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.

  • Python 2
  • Python 3
fruit = {'mango':40,'banana':10,'cherry':20}
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:

  • Python 2
  • Python 3
fruit = {'mango':40,'banana':10,'cherry':20}
print fruit['mango']
print fruit['banana']
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.

  • Python 2
  • Python 3
d = {}
d['a'] = 1
d['b'] = 2
print d
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 gives us the list of all keys and values of a dictionary respectively. Let's see an example of these functions:

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

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

  • Python 2
  • Python 3
a = {'x':[1,2,3,4],'y':"Hello World",'z':4.0}
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.

  • Python 2
  • Python 3
score = {40:"x",30:"y",50:"z",45:"a"}
for s in sorted(score):
	print s,":",score[s]
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 on 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 lists, we can also use len() function on a dictionary.

We can also use the in operator to check if a key or value is present in a dictionary or not.

Let's try this.

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

In Python 2, we have a function has_key which can be directly used to check if a key is present in a dictionary or not. This is also shown in the above picture.

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

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.