Dictionary Points
Dictionary Points
* is {key:value} pair.
* is dynamic and mutable entity.
* is represented within {}.
* every dictionary is object of class "dict".
* does not have natural/automatic indexing (0 onwards).
The keys will behave as index.
* allows over-writing of value by referring "key" as
index.
* does not maintain order of elements. (it is non-linear ds)
* Every "key" must be static and immutable object only;
whereas, "value" can be Python object.
* does not allow duplicate "keys" but duplicate "values"
are allowed.
* does not support slicing-dicing operation.
======================================================
For example:
--------------
d = { "one":1, 2:"two", "three":[3, "III"],
4:("four", "FOUR", "IV"),
}
print(type(d))
print(d)
===========================================
===========================================
================================================
For example:
-------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345
}
print("Initially d :", d) <--- will print dictionary
print("Mobile number of omkar :", d["omkar"])
# will print mobile number of ["omkar"]
d["tejas"] = 556677
# will update value of ["tejas"]
print("Updated d :", d) <--- will print dictionary
========================================
For example:
-------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
"gaurav":998877
} <--- key ["gaurav"] is already present
print("Initially d :", d)
print("Mobile number of omkar :", d["omkar"])
d["tejas"] = 556677
print("Updated d :", d)
==========================================
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
print(id(d))
d["tejas"] = 556677 <--- updating value of ["tejas"]
print("Updated d :", d)
print(id(d))
=======================================================
2. By using update():
* This is member function of class "dict".
* This function requires another dictionary object as
parameter.
* In such case:
- if specified "key" is already present, then
updates/over-writes it's value
- if specified "key" is not present, then adds them as
new {k:v} pair.
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.update({"tejas":445566}) <--- key "tejas" is already present
d.update({"amey":778899}) <--- key "amey" is not present
print("Updated d :", d)
===================================================
===================================================
1. update():
* This function requires another dictionary object as
parameter.
* In such case:
- if specified "key" is already present, then
updates/over-writes it's value
- if specified "key" is not present, then adds them as
new {k:v} pair.
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.update({"tejas":445566}) <--- key "tejas" is already present
d.update({"amey":778899}) <--- key "amey" is not present
print("Updated d :", d)
For example:
-------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.update( {"tejas":445566, "amey":778899} ) <--- multiple {k:v} pairs
print("Updated d :", d)
The output will be:
---------------------
Initially d : {'tushar': 12345, 'tejas': 708090, 'gaurav': 405060, 'omkar': 102030}
Updated d : {'amey': 778899, 'tejas': 445566, 'tushar': 12345, 'omkar': 102030,
'gaurav': 405060}
=========================================
2. copy():
* This function does not have any parameter.
* This function creates and returns a new copy of invoking
dictionary object.
For example:
-------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
print("ID of original dictionary :", id(d))
d2 = d.copy() <--- creates and returns a new copy
print("Copied d2 :", d2)
print("ID of copied dictionary :", id(d2))
==============================================
3. clear():
* This function does not have any parameter.
* This function clears all {k:v} pairs from invoking
dictionary object.
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.clear()
print("Updated d :", d)
=============================================
4. pop():
* This function requires a "key" as parameter.
* This function removes specified {k:v} pair from invoking
dictionary object.
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.pop("tejas") <--- removes {k:v} pair of "tejas"
print("Updated d :", d)
5. items():
* This function does not have any parameter.
* This function returns an object of new class "dict_items"
where each {k:v} pair will be as tuple elements.
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
a = d.items()
print(a)
print("Type of a :", type(a))
================================================
6. keys():
* This function does not have any parameter.
* This function returns all the "keys" of invoking
dictionary object.
* This function will return all the keys as element of
new class "dict_keys".
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
a = d.keys()
print(a)
print("Type of a :", type(a))
====================================================
7. values():
* This function does not have any parameter.
* This function returns all the "values" of invoking
dictionary object.
* This function returns all the "values" as object of
new class "dict_values".
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
a = d.values()
print(a)
print("Type of a :", type(a))
==================================================
8. popitem():
* This function does not have any parameter.
* This function randomly removes a {k:v} pair from
invoking dictionary object.
For example:
--------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.popitem() <--- randomly removes a {k:v} pair
print("Updated d :", d)
=====================================================
9. get():
* This function requires a "key" as parameter.
* This function obtains and returns value of specified
key.
For example:
-------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
a = d.get("tejas") <--- obtains value of ["tejas"]
print("Value is :", a)
=============================================
10. setdefault():
* This function requires two individual values as parameter.
* Where first parameter will be "key" and second parameter
will be it's "value".
* If specified "key" (first parameter) is already present,
then this function will not update it's new value.
* But if specified "key" (first parameter) is not
present in invoking dictionary object, then only adds as
new {k:v} pair.
For example:
-------------
d = {"omkar":102030, "gaurav":405060,
"tejas":708090, "tushar":12345,
}
print("Initially d :", d)
d.setdefault("tejas", 778899)
# key "tejas" is already present. Hence, will not update it's new value
d.setdefault("amey", 100200)
# key "amey" is not present. Hence, adds as new {k:v} pair
print("Updated d :", d)
=====================================================
11. fromkeys():
* This function is static member function of class "dict".
* Therefore we call it by using class-name only.
(not by invoking object).
* This function has two parameters:
first is ----> a sequence
second is (optional) ----> default values for all keys
* This function creates a dictionary which takes all
the elements of specified sequence (first parameter) as
"keys" and assigns all of them the specified default
value as their "value".
* The second parameter is optional parameter. Becuase it
is having default "None" as initialized value.
For example:
--------------
list1 = ["one", "two", "three", "four"]
d = dict.fromkeys(list1)
print(d)
=====================================================