Dictionary Python-: Dictionaries are powerful data structures, used for many purposes even within Python itself. Dictionary keys must be immutable, but any immutable object can be a dictionary key. Using keys means accessing collections of data more directly and with less code than many other solutions.
Python’s name for associative arrays or maps which it implements by using hash tables. Dictionaries are amazingly useful, even in simple programs.
Because dictionaries are less familiar to many programmers than other basic data structures such as lists and strings some of the examples illustrating dictionary use are slightly more complex than the corresponding examples for other built-in data structures
What is dictionary in Python -: Values in lists are accessed by means of integers called indices, which indicate where in the list a given value is found.
Dictionaries access values by means of integers, strings, or other Python objects called keys which indicate where in the dictionary a given value is found.
In other words we can say that the both lists and dictionaries provide indexed access to arbitrary values but the set of items that can be used as dictionary indices is much larger than and contains the set of items that can be used as list indices. Also, the mechanism that dictionaries use to provide indexed access is quite different from that used by lists.Both lists and dictionaries can store objects of any type.
Values stored in a list are implicitly ordered by their positions in the list, because the indices that access these values are consecutive integers.
We may or may not care about this ordering, but we can use it if desired. Values stored in a dictionary are not implicitly ordered relative to one another because dictionary keys are not just numbers.
If we are using a dictionary but also care about the order of the items We can use an ordered dictionary which is a dictionary subclass that can be imported from the collections module. we can also define an order on the items in a dictionary by using another data structure (often a list) to store such an ordering explicitly.
Creating the dictionary-: The dictionary can be created by using multiple key-value pairs enclosed with the small brackets () and separated by the colon (:). The collections of the key-value pairs are enclosed within the curly braces {}. As like here Dict = {“Name”: “Ayush”,”Age”: 22}
Employee = {"Name": "vivek", "Age": 29, "salary":35000,"Company":"netnic"} print(type(department)) print("printing department data .... ") print(department)
Output of this programme
<class 'dict'> printing department data .... {'Age': 29, 'salary': 35000, 'Name': 'vivek', 'Company': 'netnic'}
Accessing dictionary data -: above this example we can access the dictionary data. the data can be accessed in the list and tuple by using the indexing.
Employee = {"Name": "vivek", "Age": 29, "salary":35000,"Company":"netnic"} print(type(Employee)) print("printing Employee data .... ") print("Name : %s" %Employee["Name"]) print("Age : %d" %Employee["Age"]) print("Salary : %d" %Employee["salary"]) print("Company : %s" %Employee["Company"])
Output of this example
<class 'dict'> printing Employee data .... Name : vivek Age : 29 Salary : 35000 Company : netnic
Updating dictionary values-: In the python we can update dictionary value. we know that the dictionary is a mutable data type and its values can be updated by using the specific keys. as like
Employee = {"Name": "vivek", "Age": 29, "salary":35000,"Company":"netnic"} print(type(Employee)) print("printing Employee data .... ") print(Employee) print("Enter the details of the new employee...."); Employee["Name"] = input("Name: "); Employee["Age"] = int(input("Age: ")); Employee["salary"] = int(input("Salary: ")); Employee["Company"] = input("Company:"); print("printing the new data"); print(Employee)
output of this programme
printing Employee data .... {'Name': 'vivek', 'salary': 35000, 'Company': 'netnic', 'Age': 29} Enter the details of the new employee.... Name: ramlal Age: 25 Salary: 20000 Company: netnicorg printing the new data {'Name': 'ramlal', 'salary': 20000, 'Company': 'netnicorg', 'Age': 25}
Dictionary Operators-: Besides basic element assignment and access, dictionaries support several operations. we can define a dictionary explicitly as a series of key-value pairs separated by commas.
>>> english_to_french = {'red': 'rouge', 'blue': 'bleu', 'green': 'vert'}
len returns the number of entries in a dictionary.
>>> len(english_to_french) 3
Dictionary vs caches-: dictionaries can be used as caches, data structures that store results to avoid recalculating those results over and over. Suppose that we need a function called sole, which takes three integers as arguments and returns a result. The function might look something like this:
def sole(m, n, t): # . . . do some time-consuming calculations . . . return(result)