Skip to main content

Python | Data Structure Or Data Type (Dictionary)

Data Type Of Python

In this session we will see the new data type of python which is Dictionary.

4) Dictionary:- Dictionary is a unordered collection of mutable data type. It contains key value pair enclosed in curly braces {}. Dictionary are hashable by keys.
Dictionary keys should be unique and key should be immutable. That means we can use string, tuple, frozenset as a dictionary key but not list, dictionary, or other mutable types.
  • Empty dictionary:- 
    D = {}

  • Dictionary with multiple elements:-
    D = { 'a':10, 'b':20, 'c':30 }
    Here the part before colon is the key and the part after colon is value like this dictionary has key value pair.

  • Iteration:- 
    D = { 'a':10, 'b':20, 'c':30 }
    For i in D:
           print(i)
    => 'a'
         'b'
         'c'
    In the dictionary only keys are hashable not values therefor when you iterate dictionary you get only keys.

  • Hashable:- 
    Dictionary are hashable by keys.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D['a']) => 10
    It return the value of the respected key.

  • Updation:- 
    D = { 'a':10, 'b':20, 'c':30 }
    D['b'] = 'Hi' 
    => D ={ 'a':10, 'b':'Hi', 'c':30 }
    Here the value of key b is update.

  • Membership:-
    print('a' in D) => True
    print('d' in D) => False
Methods Of Dictionary:- 
  1. clear():- It removes all items from the dictionary.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.clear()) => {}

  2. get(key,default=none):- It return the value for a key if key is in the dictionary else it return none.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.get('a')) => 10
    print(D.get('e')) => none

  3. setdefault(key,default=none):- It insert the key with a value of default if key is not in the dictionary. It return value for the dictionary if the key is in the dictionary otherwise it return none.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(d.setdefault('a','Hi')) => 10
    print(d.setdefault('e')) => None
    print(d.setdefault('f','python')) => 'python'
    Here f is not the member of dictionary D when you use setdefault method the this key is add to the dictionary.

  4. pop(key,default):- It remove first specified key and return the corresponding value. If key is not found it return default otherwise raised KeyError.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.pop('a')) => 10
    print(D.pop('e', 'not present')) => 'not present'
    print(D.pop('f')) => KeyError

  5. popitem():- It remove and return only arbitary key,value pair as a tuple but raises key error if dictionary is empty.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.popitem()) => ('a',10)
    print(D.popitem()) => ('c',30)

  6. update(dict):- It update the dictionary with the key and value pair from another dictionary it overwrite the existing keys.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    D1 = {'a':'Hi', 'd':24, 'e':21}
    print(D.update(D1)) => D={'a':'Hi', 'b':20, 'c':30, 'd':24, 'e':21}
    Here in dictionary D and D1 a is the common key so in the output the value of a in dictionary D1 is overwrite the value of a in dictionary D.

  7. fromkeys(iterable,value=none):- It create a new dictionary with keys from iterable and values set to value which is default is none.
    For example:-
    D1 = {}
    S = 'Python'
    print(D1.fromkeys(S)) => { 'P':none, 'y':none, 't':none, 'h':none, 'o':none, 'n':none}
    Here String S is iterate and store in dictionary as a key and there value becomes none because we does not give any value to it. Now we another example with the value.
    print(D1.fromkeys('abc',10)) => {'a':10, 'b':10, 'c':10}
    Here we give the value 10 so it goes to the every key of the dictionary.

  8. keys():- It return the list of dictionary keys.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.keys()) => ['a', 'b', 'c']
     
    Here a, b and c are the keys of dictionary D.

  9. values():- It return a list of dictionary values.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.values()) => [10, 20, 30]
    Here 10, 20 and 30 are the values of dictionary D.

  10. items():- It return list of two tuple as a key value pair.
    For example:-
    D = { 'a':10, 'b':20, 'c':30 }
    print(D.items()) => [('a',10), ('b', 20), ('c', 30)]

These all are the methods of dictionary.

Comments

Popular posts from this blog

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Python | Data Structure Or Data Types (Sets)

Data Type Of Python In this section we will see new data type which is Sets. 3) Sets:- Set object is an unordered collection of distinct unhashable object. The common user include membership test removing duplicate from a sequence and computing mathematical operation such as intersection, union, differences etc.     Being an unordered collection set do not record element position or order of insertion. Accordingly set do not support indexing, slicing, stepping or other sequence like behavior.      We declare a set by using curly braces {}. Note:- Set can only store immutable data. Empty set:-  S = set() Set with multi element:- S = {10, 'Hi', (10, 20)} Membership:- print(10 in S) => True print(20 in S) => False Iteration:- for i in S:       print(i)  => 10       'Hi'       (10,20) Type casting:- S = "Programming" S2 = set(S) print(S2) => {'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'} In the ...

Python | Data Structures or Data Types (List)

Data Type of Python In Python there are two types of data types which are Mutable and Immutable. Mutable data type contains List and Dictionary data types. And Immutable data type contains String,Tuple and Sets data types. Now we see what is Mutable and Immutable data type? The main object get changed permanently after applying methods of data type this data type is called as Mutable data type. And the main object will not be changed permanently after applying methods of data type this data type is called as Immutable data types. Now we see data types one by one! 1) List - List is an ordered collection of mutable datatype. Lists are hashable, iterable. List contain any kind of data. List are declared in square braces[]. Empty List-  L = [ ] List with multiple elements -  L1 = [10, 15.10, 'Python', [10,20] ] In the above list you can add any data type as shown above. In list L1 there is Integer, float,String,list data types. To add string data type you must have to declared in ...