Saturday, 3 February 2018

iv)List :
      List is the best of the data types in python3.
      It can hold anything(Literally anything).
      Syntax :
             There are two ways you can declare an empty list:
                          list() or []

            >>> type(list())
    <class 'list'>
    >>> type([])
    <class 'list'>
>>> [1,2,"string", """ multiline \n comment """, 12.564]
[1, 2, 'string', ' multiline \n comment ', 12.564]

>>> a = [2, 5, 45.74, "python"]
>>> a[0]
2
>>> a[3]
'Python'
Python has a special provision for accessing elements from last in case the length of list is unknown.
You can access the last element by using negative indexing.
List[-n] returns nth element counting reverse , starting from the last in the List  .
>>> a[-1]
'Python'
>>> a[-3]
5
Checking length(number of elements) of list :
    len(list) is the method used to find.
        >>> len(a)
4
 
Slicing in list :
    Slicing is a process of selection of elements by using indexes.
    List[a:b] will return list of elements in List from index a to b
        Excluding element at bth position. b>=a
    If a/b parameters are not provided then python takes extreme
        Position.
        For example :
>>> a[2:4]
[45.74, 'python']
>>> a[2:1]
[]
>>> a[3:]
['python']
>>> a[:3]
[2, 5, 45.74]
>>> a[:]
[2, 5, 45.74, 'python']
Slicing with negative index :
>>> a[:-1]
[2, 5, 45.74]
>>> a[:-2]
[2, 5]
a[-x ] is same as a[len(a) - x ]
>>> a[:-1]
[2, 5, 45.74]
>>> a[:-2]
[2, 5]

Reversing a list :
>>> a[::1]
[2, 5, 45.74, 'python']
    reversed(x) can be used to reverse a list.
    Rerversed function returns a reverseiterator . So, it has
        to be type casted to list.
>>> list (reversed(a))
['python', 45.74, 5, 2]

Counting occurrences of certain element in a list.
Syntax : list.count(element_to_find_frequency_of)
Example :
>>> [1, 1, 2, 1, 2, 5, 8 ,6, ""].count(1)
3
Sorting a list :
    sort attribute can be used to sort a list.
    To use sort attribute, list cannot have any other type other than
        that of int and float.
    sort attribute does not return anything but performs sorting on
        the list.
>>> a = [1, 2, 5, 4, 3]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

Pop attribute of list :
Syntax : list.pop(index)
             Where, index is index of element you want to remove
             from the list.
This attribute returns the deleted element.
    [1, 3.23, 4, 5]
>>> a.pop(1)
3.23
>>> a
[1, 4, 5]
Copying a list :
    Suppose a=[1,2,3] is a list.
    b = a will give reference of a to b, not copying the list.
    Now, what does that mean??
    That means, when you will modify elements of a, elements of
        b will also be affected and vice versa.

    For copying the elements of a list to another, copy attribute is
        Used.
    b = a.copy()
    Now, elements in b will have elements same as a but
       modifying a or b won’t affect the other list.

Append attribute :
Append takes exactly one argument as an input that will
include the argument passed inside the attribute in the list.
>>> a
[1, 45, 3, 4, 5]
>>> a.append([1,2,3])
>>> a
[1, 45, 3, 4, 5, [1, 2, 3]]

Here, i append a list [1, 2, 3] to the list a.

Removing all the elements :
clear attribute is used.
        >>> a.clear()
>>> a
[]

List can be merged with another list using extend attribute of the list
list.extend(list1)
>>> a = [1, 2, 3, 5]
>>> a.extend([4, 6])
>>> a
[1, 2, 3, 5, 4, 6]
This is one of the way you can add a list to another list.
Another way is :
list = list + list1
>>> a = a + [9, 7, 8]
>>> a
[1, 2, 3, 5, 4, 6, 9, 7, 8]
These were some of the  attributes and functionalities you can do using list. 
More properties we will see in future posts.

No comments:

Post a Comment

Memoization : Memoization is a technique for remembering the results that incur huge costs to running time to the algorithm. Memoizati...