Pythondictionary (Dictionary)
A dictionary is a mutable container model and can store any type of object.
Each key value key=>value pairs, with colons : Split
Each key-value pair is separated by a comma,
The entire dictionary is included in curly braces {}, and the format is as follows:
D = {key1:value1, key2:value2}
Create
>>>dict = {' A ': 1, ' B ': 2, ' B ': ' 3 '};>>> dict[' B '] ' 3 ' >>> dict{' a ': 1, ' B ': ' 3 '}
Access
#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' First '}; print ' dict[' name ']: ", dict[' name '];p R int "dict[' age '):", dict[' age ');
Output
Modify
Dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' First '}; dict[' age '] = 8; # Update existing entrydict[' School '] = "DPS Sch Ool "; # ADD new entry print "dict[' age '):", dict[' age '];p rint "dict[' School ']:", dict[' School '];
Output
Delete
Dict = {' name ': ' Zara ', ' age ': 7, ' Class ': ' First '}; del dict[' name ']; # Delete key is ' name ' entry dict.clear (); # Empty dictionary all Entries del dict; # Delete Dictionary
Characteristics of the Dictionary:
The same key is not allowed to appear two times. When created, if the same key is assigned a value of two times, the latter value is remembered, as in the following example:
Dict = {' name ': ' Zara ', ' age ': 7, ' name ': ' Manni '}; print ' dict[' name ']: ", dict[' name '];
Output only after one Manni
The key must be immutable, so it can be used as a number, string, or tuple, so you can't use a list, as in the following example:
Dict = {[' name ']: ' Zara ', ' age ': 7}; print ' dict[' name ']: ", dict[' name '];
Python small white dictionary using notes