Sunday, 27 March 2016

List in Python with code samples

List in Python:
The list is one of the data type available in Python. The items in the list are separated by comma values and which are placed between square brackets.
NOTE: The items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets. For example −
My_list1 = ['String', 'String2', 12, 232]
My_list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
My_list3 = ["a", "b", "c", "d", "e", "f", "g"];

Accessing Values in Lists

By using the square brackets for slicing along with the index we can access the values in the list or we can use indices to obtain value available at that index.

if __name__ == '__main__':
   
    My_list1 = ['String', 'String2', 12, 232]
    print "My_list1[0]:", My_list1[0]
    print "My_list1[1:5]:", My_list1[1:5]

When the above code is executed, it produces the following result −
My_list1[0]: String
My_list1[1:5]: ['String2', 12, 232]

Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. For example −
if __name__ == '__main__':
   
    My_list1 = ['String', 'String2', 12, 232]
    print "My_list1[0]:", My_list1[0]
    My_list1[0] = 586
    print "New value available at index 0 : ", My_list1[0]
When the above code is executed, it produces the following result −
My_list1[0]: String
New value available at index 0 :  586

Delete List Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know
See the example below

if __name__ == '__main__':
   
    list1 = ['String', 'String2', 12, 232]
    print list1
    del list1[2];
    print "After deleting value at index 2 : ", list1
When the above code is executed, it produces following result −
['String', 'String2', 12, 232]
After deleting value at index 2 : ['String', 'String2', 232]

Basic List Operations

Tuples support concatenation and repetition here too, except that the result is a new tuple.tuples respond to all of the general sequence operations.
The below list describes the list of tuple operations that the python supports
if __name__ == '__main__':  
    EX_lst1 = ('String1', 'String2', 1997, 2000);
    EX_lst2 = (1, 2, 3, 4, 5, 6, 7 );

    print " Concatination :", EX_lst1 + EX_lst2
    print "Length:" , len(EX_lst1)
    print "Repetition:" , (EX_lst2) * 4
    print "Membership" , 3 in EX_lst2
    for i in EX_lst2:
        print i   
The result of the above code is
Concatination : ['String1', 'String2', 1997, 2000, 1, 2, 3, 4, 5, 6, 7]
Length: 4
Repetition: [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]
Membership True
1
2
3
4
5
6
7

Indexing, Slicing, and Matrixes of lists

As of we know that lists are sequences, indexing and slicing works.
Let us try to understand the below example
if __name__ == '__main__':
   
    EX_lst1 = ['String1', 'String2', 'String3', 'String4']
    EX_lst2 = [1, 2, 3, 4, 5, 6, 7 ]

    print " Offsets start at zero:", EX_lst1[2]
    print "Negative: count from the right:" , EX_lst1[-2]
    print "Repetition:" , (EX_lst2) * 4
    print "Slicing fetches sections" , EX_lst2[1:]
The output of the above program is
Offsets start at zero: String3
Negative: count from the right: String3
Repetition: [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]
Slicing fetches sections [2, 3, 4, 5, 6, 7]

Built-in List Functions & Methods:




Python includes the following list functions −




         
if __name__ == '__main__':
    
    EX_tup1 = (1, 2, 3, 4, 5, 6, 7 )
    EX_lst1 = ['String1', 'String2', 1997, 2000]
    EX_lat2 = [1, 2, 3, 4, 5, 6, 7 ]

    print "Length:" , len(EX_lat2)
    print "Minimum value:" , min(EX_lat2)    
    print "Maximum value:" , max(EX_lat2)
    print "Convert list to tuple:" ,   list(EX_tup1)

The output of the above example code is 




Length: 7
Minimum value: 1
Maximum value: 7
Convert tuple to list: [1, 2, 3, 4, 5, 6, 7]

Python includes following list methods




from itertools import count

         
if __name__ == '__main__':
    
    EX_tup1 = (1, 2, 3, 4, 5, 6, 7 )
    EX_lst1 = ['String1', 'String2', 1, 1, 2, 3, 4, 5, 6, 7 ]
    EX_lst2 = [1, 2, 3, 4, 5, 6, 7 ]

    print "Appends object obj to list:" , EX_lst1.append(EX_lst1)
    print "count of how many times obj occurs in list:", EX_lst1.count(1)    
    print "Appends the contents of EX_lat2 to EX_lst1:" , EX_lst1.extend(EX_lst2)
    print "Returns the lowest index in list that obj appears", EX_lst1.index(1)
    print "Inserts object obj into list at offset index", EX_lst1.insert(2, EX_lst2)
    print "Removes and returns last object or obj from list", EX_lst1.pop(EX_lst1[-2])
    print "Removes and returns last object or obj from list", EX_lst1.remove (1)
    print "Reverses objects of list in place", EX_lst2.reverse()


No comments: