Python dictionaries are just like a hash table, where an object is mapped to a key name.
1. Accessed by keyword, not an offset. Dictionaries are similar to associative arrays. Each item in the dictionary has a corresponding keyword; the keyword is used to “call” the item.
2. Stored objects are in a random order to provides faster lookup. when created, a dictionary stores item in any order it chooses.to get a value, simply supply the key. If you need to order the item within a dictionary, you have to do it yourself; there are no built-in methods for it.
3. Dictionaries are variables length, can hold objects of any type , and support deep nesting.
4. They are mutable but can’t be modified like lists or strings; they are the only data type that supports mapping.
5. Internally, dictionaries are implemented as a hash table.

List of common operation:
⦁ d1={} Empty dictionary
⦁ d2={‘spam’:2,’eggs’:3} Two-item dictionary
⦁ d3={‘food’:{‘harm’:1,’egg’:2}}Nesting
⦁ d2[‘eggs’],d3[‘food’][‘ham’]Indexing by key
⦁ d2.has_key(‘eggs’),d2.keys(),d2.values() Methods: membership test,key lists,values list,etc.
⦁ len(d1) Length(number stored entries)
⦁ d2[key]=new,del d2[key] Adding/changing,deleting

Making a dictionary:
As stated,you create dictionaries and access item via a key.
The key can be of any immutable type, like a string, number, or tuple. The values can be any type of object, including other dictionaries.the format for making a dictionary is:

>>>dictionary={"key name":"value"}

Example for Dictionary:

Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"} 
print("printing Employee data .... ") 
print("Name : %s" %Employee["Name"]) 
print("Age : %d" %Employee["Age"]) 
print("Salary : %d" %Employee["salary"]) 
print("Company : %s" %Employee["Company"]) 
<class 'dict'>
printing Employee data .... 
Name : John
Age : 29
Salary : 25000
Company : GOOGLE

Deleting elements using del keywords:

Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"} 
print("printing Employee data .... ") 
print("Deleting some of the employee data") 
del Employee["Name"] 
del Employee["Company"] 
print("printing the modified information ") 
print("Deleting the dictionary: Employee"); 
del Employee 
print("Lets try to print it again "); 
<class 'dict'>
printing Employee data .... 
{'Age': 29, 'Company': 'GOOGLE', 'Name': 'John', 'salary': 25000}
Deleting some of the employee data
printing the modified information 
{'Age': 29, 'salary': 25000}
Deleting the dictionary: Employee
Lets try to print it again 
Traceback (most recent call last):
  File "list.py", line 13, in <module>
NameError: name 'Employee' is not defined

Iterating Dictionary:

Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"} 
for x in Employee: 

Translate »