Python dictionaries are unordered collection of items used to store data values. It is changeable and indexed and is written in curly brackets and has keys and values. Keys are the single values. Values can be list or list within the list. The dictionaries are the data types which are similar to the array but it will work with the values and keys instead of indices. Each value stored in dictionary and can be accessed using the keys which act as object. The key value is provided in dictionary to make optimized and each key value and separated by colon’:’ there each key is separated by comma and it is enclosed in curly brace.
An empty dictionary is written with two curly braces like {}.
The dictionaries are unique and immutable data types as strings, integer and tuples. The values of dictionary may be of any type such as string, numbers. They provide another composite data type called as dictionary. The dictionaries are another example of data structures and used to map or associate things you want to store keys you need.
Ways for creating dictionaries are as follows:-
Creation of empty list is done by two methods as:-
#creation of the empty dictionaries by the empty braces.
e.g:-
my_dict={}
#creation of the empty dictionaries by using dict().
my_dict=dict()
O/p:-
{}
b)dictionary with literals:-
Creation of the dictionary by the set of keys of literals.
e.g:-
my_dict={1:’ball’,3:’apple’}
O/p:-
{1:’ball’, 3,’apple’}
c) Creation of dictionary by two lists:
suppose we have two sets:-
e.g:-
#list of strings
listofStrings = [“hi”, “hello”, “there”, “at”, “when”]
#list of integers
listofInts = [10, 45, 23, 77,67]
Now we will use elements in list of strings and item in list of int as value.
Zip function is used here to, merge the two sets.
O/p:-
{‘hi’:10,’hello’:45,’when’:67,’at’:77,’there’:23}
d) Creating dictionary by passing parameter in constructor:-
Now we create the dictionary by passing the values in dictionary constructors:-
e.g:-
my_dict=dict(ball=1,
this=22,
is=33,
are=27,
there=28)
O/p:-
{‘there’:28,’ball’:1,’this’:22,’is’:33,’are’:27}
E) Creating dictionary by list of initializing of keys by same values:-
Suppose we have list of strings as,
e.g:-
[“hi”,”hello”,”when”,”at”,”there”]
Now we create a dictionary where all elements of list will be key and default value will be 1.
O/p:-
{'hi': 1, 'hello': 1, 'when': 1, 'at': 1, 'there': 1}
The accessing of the elements in dictionaries is as follows:-
While indexing with container type to access values dictionary uses keys.
Keys are used inside square bracket or get() method it will return none if key not found.
e.g:-
my_dict = {'addr':'katraj', 'city': ‘pune’}
# Output:
katraj
print(my_dict['addr'])
# Output:
city
print(my_dict.get('pune'))
After running the code the
O/p:-
Katraj
City
Methods of dictionaries are given as follows:-
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
thisdict.update({"model": "Honda1"})
O/p:-
{‘brand’:’honda’,’model’:’shine’,’year’:2019,’color’:’blue’}
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
thisdict.popitem()
print(thisdict)
O/p:-
{‘brand’:’honda’,’model’:’shine’}
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
del thisdict["model"]
print(thisdict)
O/p:
{‘brand’:’honda’,’year’:2019}
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
thisdict.clear()
print(thisdict)
O/p:
{}
To check how many items are there in dictionary by using the len() method.
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
print(len(thisdict))
O/p:-
3
6. Return the values:-
The method values () returns a view object.
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
x=thisdict.values
print(x)
O/p:-
Honda
Shine
2019
7. setdefault the values:-
The setdefault() method return item with specified key. If the key does not exit etc.
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
x = car.setdefault("model", "Splender")
print(x)
O/p:-
Splender
You can also use loop through dictionary by using the for loop.
While looping the return value are keys of dictionary but method returns the values as well.
e.g:-
thisdict = {
"brand": "honda",
"model": "shine",
"year": 2019
}
for x in thisdict:
print(x)
O/p:-
Honda
shine
2019