Python Dictionaries
Here, we will learn about dictionaries, the operators and methods used on dictionaries. It is nearly inconceivable to work with python program or scripts, without lists and dictionaries. Python dictionaries can be changed easily at runtime.
Dictionary is like a list, but in a general form. It can be think like this, dictionary is a mapping between set of indexes or keys with set of values, where each key maps to a value. Combination of a key and its value is called a key value pair or item.
Suppose we take an example, and build a dictionary which maps words from English to French. So the keys and their values will be all strings.
In Python dictionary, each key is separated by a colon (:) from its values. All the items are separated by comas and the whole dictionary is enclosed within ‘{‘ and ‘}’. In the dictionary, all the keys should have to be unique with data type as strings, tuples or numbers and the values can be of any of these three type.
Define An Dictionary In Python
Example:
#!/usr/bin/python
dicto = {'Bookname' : 'Python', 'Price' : 210}
Accessing Dictionary Values
Dictionaries values can be accessed by using the square braces i.e. [ ] along with the key name to obtain the value. This is multiple item declaration procedure used to declare keys along with their values in Python’s dictionary.
Example:
#!/usr/bin/python
dicto = {'Bookname' : 'Python', 'Price' : 210}
print dicto['Bookname']
print dicto['Price']
When the above code gets executed the outcome will be:
Output:
Python
260
Creating A New Dictionary In Python
Example:
#!/usr/bin/python
new_dict = dict()
print new_dict
or simply write
#!/usr/bin/python
new_dict = {}
print new_dict
Output:
{}
The function ‘dict’ creates a new dictionary with no items within it. This is because ‘dict’ is the name of a built-in function and programmers should avoid in using it as variable name. Furthermore the curly braces { & } together represents an empty dictionary. So to use the dictionary to add items Python programmers use square braces such as:
Example:
#!/usr/bin/python
new_dict = dict()
new_dict['First'] = 10;
new_dict['Second'] = 20;
For mapping a single item of a dictionary, programmers can use this procedure also. This is one-by-one procedure of declaring items. The line creates an item that maps from key ‘first’ to the value 10. Similarly in second case also.
Dictionary As Set Of Counter
Let us consider a situation where programmers are given and they have to count how many times each letter appears.
To do this the techniques are:
It is based on the programmer what & how he/she wants to implement things into programming. An implementation is the method of doing or performing a computation based on your conception. In the above scenario, there is an advantage while using dictionaries – that we do not have to think or know ahead of which letter appears in the string & just have to allot space & room for those letters.
Updating Dictionary In Python
Programmers can update or modify the existing dictionary by simply adding new entry or a key-value pair or by deleting an item or entry.
The code describes it below:
Example:
#!/usr/bin/python
dicto = {'Bookname' : 'Python', 'Price' : 210}
#Adding new entries
dicto ['Author'] = 'TutorialsCloud' ;
dicto ['Discount']= '10 Percent';
#Updating an Entry
dicto ['Price'] = 200;
Deleting Elements From Dictionary
Deleting elements from a dictionary can be done either removing individual elements or use clear() to clear the entire dictionary’s content.
Example is show below:
Example:
#!/usr/bin/python
dicto = {'Bookname' : 'Python', 'Price' : 210, 'Author': 'TutorialsCloud'}
del dicto['Price'];
# It deletes only the key with the name 'Price'
dicto.clear();
# The above code removes all entries in dictionary & makes the dictionary empty
del dicto
# The above code Deletes the entire dictionary
Comments
Post a Comment