Skip to main content

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 single or double quotes.
  • Hashable- 
    print (L1[2]) => 'Python'
    In the above example it returns 3rd element of list. In the above example we entered index number 2 in the square braces. To know more about the example please refer blow table. 

     0
    3
     10
    15.10 
    'Python' 
    [10,20] 
     -4
     -3
     -2
     -1
     
    We will see another example
    print(L1[0]) => 10
    print(L1[0:3]) => [10, 15.10, 'Python' ]
    Now in this example it will print from index number 0 to index number 2. It will return first three elements of list L1.
  • Iterable-
    for i in L1:
           print(i)
    It will return => 10 
                         => 15.10
                         => 'Python'
                         => [10,20]
  • Membership-
    print(10 in L1) => True
    print(20 in L1) => False
    print(20 not in L1) => True
Methods of List:-
  1. append(obj) - It append and object to the end of the list.
    For example:-
    L = [ 1, 2, 3 ]
    L.append(4) => [ 1, 2, 3, 4]
    L.append('Hello') => [ 1, 2, 3, 4, 'Hello' ]

  2. clear():- It removes all elements from list.
    For example:-
    L=[ 1, 2, 3, 4, 'Hello' ]
    L.clear() => []
    When you apply clear method to list only elements are deleted from list memory allocation is not remove.

  3. count(value):- It count number of occurrences of value in a list.
    For example:-
    L=[ 1, 2, 3, 4, 2, 3, 2, 1 ]
    print(L.count(3)) => 2 
    Here the value 3 is occur twice in the list.
    print(L.count(2)) => 3
    Here the value 2 is occur three times in the list. 

  4. index(value,start,stop):- It return first index of given value. It raises value error if the value is not present. The start and stop is for slice notation.
    For example:-
    L=[ 1, 2, 3, 4, 2, 3, 2, 1 ]
    print(L.index(3)) => 2
    In this example the value 3 is repeat twice but index method return the index number of first occurrence.
    print(L.index(3,3)) => 5
    In this example it will find the value 3 from the index number 3 not from the index number 0.
    print(L.index(1,2,5)) => valueError
    In this example the value 1 is not found between index number 2 and index number 5 so it will return value error.

  5. extend(iterable):- It extends the list by appending the elements from iterable. Each elements of an iterable is placed on individual indexes.
    For example:-
    L=[ 1, 2, 3 ]
    L.extend([ 4, 5, 7 ]) => [ 1, 2, 3, 4, 5, 7 ]
    L.extend( 'Hello' ) => [ 1, 2, 3, 4, 5, 7, 'H', 'e', 'l', 'l', 'o' ]

  6. insert(index,obj):- It insert an object at given index. The original value at that index is right shifted.
    For example:-
    L=[ 10, 20, 40, 60 ]
    L.insert( 2, 30 ) => [ 10, 20, 30, 40, 60 ] 
    In the above example we add 30 to the index number two but in original list have value 40 at the index number 2. When we perform insert method the original value of that index is shifted to right side.

  7. pop(index=-1):- It remove and return the item at given index. Default index is last index. It raises index error if index is out of range or list is empty.
    For example:-
    L=[ 10, 20, 30, 40, 50, 60 ]
    print(L.pop()) => 60
    print(L) => [ 10, 20, 30, 40, 50 ]
    print(L.pop(-2)) => 40
    print(L) =>[ 10, 20, 30, 50 ]

  8. remove(value):- It removes the first occurrence of given value. It raises value error if the value is not present.
    For example:-
    L=[ 10, 10, 20, 30, 20, 40, 30 ]
    L.remove(20) => print(L) => [ 10, 10, 30, 20, 40, 30]

  9. reverse():- It reverse a list by in place.
    For example:-
    L = [ 'Hi', 2, 5, 3, 9, 'Bye', 'World', 4, 2, 1 ]
    L.reverse() => [ 1, 2, 4, 'World', 'Bye', 9, 3, 5, 2, 'Hi' ]

  10. sort(key=none, reverse=false):- It sort the list in ascending order default. If we have multiple data types in a list we use key.
    For example:-
    L=[ 'Hi', 2, 5, 3, 9, 'Bye', 'World', 4, 2, 1 ]
    L.sort(key=str, reverse=True) => ['World', 'Hi', 'Bye', 9, 5, 4, 3, 2, 2, 1]

    L1=[ 10, 10, 20, 30, 20, 40, 30 ]
    L1.sort() => [ 10, 10, 20, 20, 30, 30, 40 ]

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 ...