dictionary blog surya
PYTHON PROGRAMMING - DICTIONARY
DICTIONARY:
1 . Dictionary is an unordered sequence of data of key-value pair form.
2. Dictionaries are written within curly braces in the form key:value.
3. It is very useful to retrieve data in an optimized way among a large amount of data.
EXAMPLE:
DICTIONARY METHODS:
SYNTAX: dict.clear( )
EXAMPLE:
2. COPY( ):
The copy( ) method returns a shallow copy of the dictionary. The copy( ) method doesn't take any parameters. This method returns a shallow copy of the dictionary. It doesn't modify the original dictionary.
3. FROMKEYS( ):
The fromkeys( ) method creates a new dictionary from the given sequence of elements with a value provided by the user. This method takes two parameters sequence and value(optional).
SYNTAX: dict.fromkeys(sequence[, value])
EXAMPLE:
4. GET( ):
The get( ) method returns the value for the specified key if key is in dictionary. This method takes maximum of two parameters key and value(optional).
SYNTAX: dict.get(key[, value])
5. ITEMS
The items( ) method returns a view object that displays a list of dictionary's (key,value) tuple pairs. This method doesn't take any parameters.
SYNTAX: dictionary.items( )
EXAMPLE:
6. KEYS( ):
The keys( ) method returns a view object that displays a list of all the keys in the dictionary. When the dictionary is changed, the view object also reflects these changes. This method doesn't take any parameters.
SYNTAX: dict.keys( )
7. POP( ):
The pop method( ) removes and returns an element from a dictionary having the given key. This method takes two parameters key and default.
SYNTAX: dictionary.pop(key[, default])
EXAMPLE:
8. POPITEM( ):
The python popitem( ) method removes and returns the last element (key, value) pair inserted into the dictionary. This method doesn't take any parameters.
SYNTAX: dict.popitem( )
EXAMPLE:
9.SETDEFAULT()
The setdefault( ) method returns the value of a key (if the key is in dictionary). If not, it inserts key with a value to the dictionary.
SYNTAX: dict.setdefault(key[, default_value])
10. UPDATE( ):
The update( ) method updates the dictionary with the elements from the another dictionary object or from an iterable of key/value pairs. This method adds element(s) to the dictionary if the key is not in the dictionary, it updates the key with the new value
1. The update( ) method takes either a dictionary or an iterable object of key/value pairs(generally tuples).
2. If update( ) method is called without passing parameters, the dictionary remains unchanged.
SYNTAX : dict.update([othe
EXAMPLE:
11.VALUES()
The values( ) method returns a view object that displays a list of all the values in the dictionary. This method doesn't take any parameters.
SYNTAX: dictionary.values( )
EXAMPLE:

Comments
Post a Comment