Linux

Python Dictionary

A Python dictionary is written as a sequence of key/value or item pairs separated by commas. These pairs are sometimes called entries. Example
Syntax of Python dictionary
 Dictionary_name = { key  : value  }  
The key and value pair is called item, key and value is separated by colon ( : ). The Each item is separated by comma ( , ), whole thing is enclosed in curly braces ( { } ). An empty Python dictionary can be create by just tow curly braces { } .

Features of Python dictionary
  1. The key of Python dictionary can not be changed.
  2. The value of key can be changed.
  3. Key as well as value can be of string and integer type.
  4. Ordering is not significant, the order in which you have entered the items in dictionary, may not get the items in same order.

Operations on Python dictionary

In this section you will learn the operations of Python dictionary.

Accessing the values of Python dictionary

You will learn how to access the value from dictionary.
Accessing value from Python Dictionary
Accessing value of the Python dictionary
The above figure shows that, in order to access dictionary elements, use the square brackets along with the key.

Delete Elements of Python dictionary

Syntax
del dict[key]

You can delete single element of dictionary as well entire dictionary. Example
Delete Elements of Python Dictionary
deleting value of the Python dictionary
You can see the error, because dictionary named port does exist after command del port .

Updating Python dictionary

Syntax
dict[key] = new_value

You can update the value of dictionary. just specify its key in square bracket, just reassign the value to old key Example
updating value of the Python Dictionary
updating value of the Python dictionary
You can not set more than one value to one key. If you do this last assign value is considered as real value.

Adding item to Python dictionary

Syntax
dict[new_key] = value

In Python dictionary add value is very easy just specify key in [] as shown in example.
Example
adding value of the Python Dictionary
adding value of the Dictionary
just specified new key and assign new value to it.


Dictionary functions

In this section you will learn the some Python dictionary functions.

len()

Syntax
len(dict)

The function len() gives the total number of item(s) in the Python dictionary.
Example
len() returns Python dictionary's length
Dictionary's length

str()

Syntax
str(dict)

The function str() converts a dictionary into a string
Example
Python Dictionary converted into string
Dictionary converted into string

cmp()

Syntax
cmp(A, B)

Lets check its functionality by example one by one.
Example 1
Python Dictionary's Comparison
Python Dictionary's Comparison
If length and items are same then it returns 0
Example 2
Python Dictionary's Comparison
Python Dictionary's Comparison
The function cmp() first compares the dictionaries by their length.
Example 3
Python Dictionary's Comparison
Python Dictionary's Comparison
If length of both dictionaries are equal then method Next, find the key asmkey in A that is the smallest key for which A[asmkey] != B[asmkey] . Also find the smallest key bsmkey in B for which A[bsmkey] != B[bsmkey]. If asmkey != bsmkey , then return cmp(asmkey, bsmkey). Else return cmp(A[asmkey], B[bsmkey]).

Python Dictionary methods

Here are some dictionary methods.

copy()

Syntax
dict.copy()

If you want to make a copy Python dictionary use copy() method.
Python Dictionary copy()
Dictionary's copy
don't confused with copy with assignment operator.
see the figure below
Python dictionary copy with assignment operator
Dictionary's copy and assignment operator
If any item is deleted from port dictionary then it is also reflected by port2.

get()

Syntax
dict.get(key, default=None)

The method get() is used to get the value of given key, if key is not found then default value or message would return.
get the Python dictionary item
get the dictionary items
When key is found; it prints its value but when key is not found; it prints set message "Value not found"

has_key()

Syntax
dict.has_key(key)
key -- This is the Key to be searched in the Python dictionary.

The method has_key() is used to check whether a given is available in the dictionary or not. If key is found in the dictionary then it returns true, false otherwise.
Checking key in dictionary with has_key()
has_key()

items()

Syntax
dict.items()
The method items() returns the list of dict's (key, value) tuple pairs.
Python dictionary's items
dict.items()

keys()

Syntax
dict.keys()
The method keys() returns all keys of Python dictionary.
key() return Python's key
keys()

value()

Syntax
dict.value()
The method value() returns all values of Python dictionary.
value() return Python dictionary values
values()

setdefault()

Syntax
dict.setdefault(key, default=None)

key -- This is the key to be searched.
Default -- This is the Value to be returned in case key is not found. The method setdefault() is similar to get() method. If value is not found in Python dictionary, default value has been added.
setdefault() of Dictionary
setdefault()
When we set "None", it shows nothing but set key's values "None". We can set any value if key is not found in the Python dictionary.

update()

Syntax
dict.update(dict2)

dict2-- This is the dictionary to be added.
consider if you want to add one Python dictionary to another you can take advantage of update() method
merged one dictionary into another by update()
merged one dictionary into another
In above example port is update with new port1. In this way you can add one dictionary to another dictionary.




admin