A dictionary is a collection, whose values are accessible by key.It is a collection of name value pairs.
Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}
A dictionary is a more general version of a list. Here is a list that contains the number of days in the months of the year:
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
If we want the number of days in January, use days[0]. December is days[11] or days[-1].
Here is a dictionary of the days in the months of the year:
days = {'January':31, 'February':28, 'March':31, 'April':30, 'May':31, 'June':30, 'July':31,'August':31,'September':30, 'October':31, 'November':30, 'December':31}
To get the number of days in January, we use days['January']. One benefit of using dictionaries here is the code is more readable, and we don’t have to figure out which index in the list a given month is at. Dictionaries have a number of other uses, as well.
We’ll build a dictionary that maps A to 100 and B to 200, so the keys are strings and the values are all Integer.
Creating dictionaries Here is a simple dictionary:
d = {'A':100, 'B':200}
We can also create a dictionary using function dict creates a new dictionary with no items. Because dict is the name of a built-in function, you should avoid using it as a variable name.
>>> dictionary = dict()
>>> print(dictionary)
{}
The curly brackets, {}, represent an empty dictionary.
To declare a dictionary we enclose it in curly braces, {}. Each entry consists of a pair separated by a colon. The first part of the pair is called the key and the second is the value. The key acts like an index. So in the first pair, 'A':100, the key is 'A', the value is 100, and d['A'] gives 100.Keys are often strings, but they can be integers, floats, and many other things as well. You can mix different types of keys in the same dictionary and different types of values, too.
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. Following is a simple example.
dt = {1: 'one', 2: 'two', 3: 'three'}
print(dt[1])
If we attempt to access a data item with a key, which is not a part of the dictionary, we get an error as follows-
<iframe>
dict = {1: 'one', 2: 'two'}
print(dict[1])
pirnt(dict[4])
---------------------------
Output:
one
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
KeyError: 4
<iframe>
Some basic operation with dictionary
d = {1: 'one', 2: 'two'}
1.Changing values of key.
Syntax: dictionary[key] = value
Example-
d[1] = 'one changed'
2.Adding new entry.
Syntax: dictionary[key] = value
Example-
d[3] = 'three'
3.Deleting entry.
del is in built operator.
Syntax: del dictionary[key]
Example-
del d[3]
Empty dictionary The empty dictionary is {}, which is the dictionary equivalent of [] for lists or '' for strings.
Important note : The order of items in a dictionary will not necessarily be the order in which put them into the dictionary. Internally, Python rearranges things in a dictionary in order to optimize performance.
0 Comments