Learn Python Fundamental in 30 Days — Day 5 (Dictionaries)

Dictionaries are unordered mapping from unique immutable keys to mutable values. Dictionaries are delimited with curly braces and contain a comma separated key-value pairs with each pair tied together by a colon.

>>> test = {“fname”:”pra”,”lname”:”lak”,”age”:33}
>>> test
{‘fname’: ‘pra’, ‘lname’: ‘lak’, ‘age’: 33}

Values are accessible by keys

>>> test["fname"]
'pra'

We should never rely on the order of items in the dictionary(it’s random) and may even vary between different runs of the same program.

Similar to list, dictionary copying is shallow by default copying only the references to the key and value objects, not the value objects themselves

Two ways we can copy the dictionary

1: Use Copy method

>>> test1 = test.copy()
>>> test1
{‘fname’: ‘test’, ‘lname’: ‘lak’, ‘age’: 33}

2: Simply passing it to dict constructor

>>> test2 = dict(test)
>>> test2
{‘fname’: ‘test’, ‘lname’: ‘lak’, ‘age’: 33}

Updating Dictionary

>>> test[“value”] = 5
>>> test
{‘fname’: ‘test’, ‘lname’: ‘lak’, ‘age’: 33, ‘value’: 5}

Dictionaries are iterable and we can used it with loops

>>> for i in test:
...     print("Values of Keys " + i + " => " + str(test[i]))
...
Values of Keys fname => test
Values of Keys lname => lak
Values of Keys age => 33
Values of Keys value => 5

If we are looking for keys then we have keys method

>>> test.keys()
dict_keys([‘fname’, ‘lname’, ‘age’, ‘value’])

Same way we can only look for values

>>> test.values()
dict_values([‘test’, ‘lak’, 33, 5])

To get hold of both keys and values we have items method

>>> test.items()
dict_items([('fname', 'test'), ('lname', 'lak'), ('age', 33), ('value', 5)])

Just to sum up

  1. Keys
  • must be unique
  • immutable type(int,float,string,tuple,bool)
  • careful with float type as a key(if float have accuracy issue, we may not be able to find out what I am looking)

2. values

  • any type(immutable and mutable)
  • can be duplicate
  • dictionary values can be lists,even other dictionaries

3. No order to keys and values

Python Standard Library provides pprint(pretty print)module which knows how to pretty print all built in data structures including dict

>>> from pprint import pprint
>>> pprint(test)
{‘age’: 33, ‘fname’: ‘test’, ‘fnamee’: 1, ‘lname’: ‘lak’, ‘value’: 5}

Dictionary Comprehension

Dict comprehension allow us to express the creation of dictionaries at runtime

Syntax

{key: value for(key,value) in iterable}

Dict comprehension is pretty useful if we want to invert the dictionary(but it’s not as common as list comprehension as it make code harder to read)

>>> import pprint
>>> D1 = {‘a’:1, ‘b’:2, ‘c’:3}
>>> D2 = {v: k for k,v in D1.items()}
>>> pprint(D2)
{1: ‘a’, 2: ‘b’, 3: ‘c’}

So this end of Day5, In case if you are facing any issue, this is the link to Python Slack channel https://devops-myworld.slack.com
Please send me your details
* First name
* Last name
* Email address
to devops.everyday.challenge@gmail.com, so that I will add you to this slack channel