Monday, January 15, 2018

Dictionaries in Python

Hello Guys,

          In this particular post, I am going to discuss dictionaries in python. The example in this post is tested on Python-3.6.

          Python data structures include lists, dictionaries, tuples, and sets. If you have worked with python, then you may have worked on "lists", "tuples", "sets" and "dictionary" in python.

Dictionary - As the name suggests dictionary is the same as that ordinary dictionary where we will have index and then we'll use that index to search the specific entry. This is similar to book or phonebook index where we search things using names instead of values which we do in other python data structure. Dictionary worked in "key":"value" pair. The "word" is called key and the definition of "word" is called a value.
              Usually, the "values" or "keys" of a dictionary are not in specific order. Another import thing is that we can't sort a dictionary in python, but can make use of the list to sort dictionaries. We will this in the example. Apart from this, we can do all add, remove and modify operations on python dictionary.

Let's go through below given example and see the basic usages in practical-


Code(you can see this on github as well)

==========================================================================

print ("\n Lets Play with Dictionary: ")

#Lets make a phone book:
#Define an empty dictionary

ages = {}
#Add few names to the dictionary
ages['Sam'] = 87
ages['Vik'] = 28
ages['Vishu'] = 54
ages['ND'] = 45
ages['Bunti'] = 37

# Display current Dictionary
print("*** Current Dictionary is :\t", ages)
#Use IN operator to check if specifid key is there in Dictionary. Syntax will be of this form:
#" if in "
#this will returns TRUE, if the dictionary has key-name in it
#but returns FALSE if it doesn't. for key, value in ages.items():

if "Sam" in ages:
    print ("\nSam is in the dictionary. He is", \
ages['Sam'], "years old")
else:
    print ("\nSam is not in the dictionary")

#Use the function keys() -
#This function returns a list of all the names of the keys.
#E.g.

print ("\nThe following guys are in the dictionary:\t", \
ages.keys())

#You could use this "keys" function to put all the key names in a list:
keys = ages.keys()

#You can also get a list of all the values in a dictionary.
#You use the values() function:
print ("People are aged the following:\t", \
ages.values())

#Now put all values i.e. ages associated with each guy in a list:
values = ages.values()

#You can sort lists, with the sorted() function. 
#It will sort all values in a list. alphabetically, numerically, etc...
#You can't sort dictionaries -they are in no particular order
print ("\nUnsorted Keys:\t", keys)
print ("Sorted Keys for Dictionary:\t", sorted(keys))
print ("\nUnsorted Values:\t", values)
print ("Sorted Values for Dictionary:\t", sorted(values))

#You can find the number of entries with the len() function:
print ("\nThe dictionary has", \
len(ages), "entries in it")
==========================================================================

Output
=====
 Lets Play with Dictionary:
*** Current Dictionary is :      {'Sam': 87, 'Vik': 28, 'Vishu': 54, 'ND': 45, 'Bunti': 37}

Sam is in the dictionary. He is 87 years old

The following guys are in the dictionary:        dict_keys(['Sam', 'Vik', 'Vishu', 'ND', 'Bunti'])
People are aged the following:   dict_values([87, 28, 54, 45, 37])

Unsorted Keys:   dict_keys(['Sam', 'Vik', 'Vishu', 'ND', 'Bunti'])
Sorted Keys for Dictionary:      ['Bunti', 'ND', 'Sam', 'Vik', 'Vishu']

Unsorted Values:         dict_values([87, 28, 54, 45, 37])
Sorted Values for Dictionary:    [28, 37, 45, 54, 87]


The dictionary has 5 entries in it

Integrate Jenkins with Azure Key Vault

Jenkins has been one of the most used CI/CD tools. For every tool which we are using in our daily life, it becomes really challenges when ...