☞It is a data structure.
☞It is represented by { } curly braces.
☞It is an unordered collection of items.
☞Each item is a key-value pair.
☞Each key map to a value.
☞Each key is separated from its value by a colon (:), the items are separated by commas (,).
☞Keys are unique while values may not be.
☞Keys must be of an immutable data type(string, number or tuple) while values can be of any type.
☞Keys are case-sensitive.
Syntax :
variable_name = { key1 : value1, key2 : value2, ………..keyN : valueN }
☞Creating an empty dictionary :
Example :
D1 = {} #empty dictionary
print(D1)
D2 = dict() #empty dictionary
print(D2)
{}
{}
☞Creating a dictionary:
Example :
D = {10 : [25.6, 35.6], "A" : "hello", (10,20,30) : "Bye" , 45.5 : "Thanks"}
print(D)
{10: [25.6, 35.6], 'A': 'hello', (10, 20, 30): 'Bye', 45.5: 'Thanks'}
☞Adding an item to the dictionary :
Example :
D = {}
D[12] = "Twelve"
D[15] = "Fifteen"
D[17] = "Seventeen"
print(D)
{12: 'Twelve', 15: 'Fifteen', 17: 'Seventeen'}
☞Creating copy of the dictionary :
Example :
D = {12: 'Twelve', 15: 'Fifteen' }
A = dict(D)
print(A)
{12: 'Twelve', 15: 'Fifteen'}
☞Creating a dictionary by passing nested list to dict() function :
Example :L = [ ['one' , 1] , ['two' , 2] , ['three', 3] ] A = dict(L) print(A)
{'one': 1, 'two': 2, 'three': 3}