Python: Data Collections - Lists


Like other programming languages, Python supports collection data types like arrays and dictionaries. In these series of posts, we are only looking at the features of Python 3.x where there is no longer any array data type in this version. However, there is no need to worry as the List data type in Python 3.x has all features of an array plus more.Lists are initialized like an array but Python 3.x makes it very easy to add, update and remove elements which can be done as individual elements or as slices.

When dealing with collections it is important to discuss the concept of slices. A slice refers to a group of consecutive elements of the list on which a single operation can be applied. As an example, if you have to divide elements from indexes 1 through 10 by of an array 'numbers' by 2 and transfer the results to a new array 'div', the operation can be performed by the following statement:

div[0:9] = numbers[1:10]/2

In languages that don't support slices, a similar operation will require the use of a loop. FORTRAN was the first language to use slices and it gave programmers tremendous power in array operations.

We now look at code samples to perform the various operations on Python lists. Note that we declare the same functions in a class to adhere to object-oriented semantics, and in-line comments explain what different functions and lines of code are doing. You can easily copy/paste the code in your Python code.

class DemoClass:
   #List is same as Array in Python
class DemoListClass:
    # List Initialization
    def Initialize(self):
        #lists are defined between square brackets '[]'
        self._listStr=["One", "Two", "Three", "Four"]
        self._listNumber = [1, 2, 3, 4]

    # List Print -  values at specific indexes
    def PrintInit(self):
        print("------ PrintInit");
        print("String list element 0 :" + self._listStr[0])
        print("String list element 3 :" + self._listStr[3])
        print("Number list element 3 :" + str(self._listNumber[3]))

    # List Modify - Add
    def ModifyElements_Add(self):
        print("------ ModifyElements_Add");
        # Add 4 elements directly
        self._listNumber.extend([5, 6, 7, 8])
        print("All elements of listNumber", *self._listNumber)
        #NOTE: If individual elements have to be added, use _listNumber.append instead
        # Add 4 elements through another list
        localList1 = [9,10,11,12]
        self._listNumber.extend(localList1)
        print("All elements of listNumber: ",*self._listNumber)
        # Add 8 elements by adding 2 lists
        localList2 = [13, 14, 15, 16]
        localList3 = [17, 18, 19, 20]
        self._listNumber.extend(localList2 + localList3);
        print("All elements of listNumber: ", *self._listNumber)

    # List Modify - Delete
    def ModifyElements_Delete(self):
        print("------ ModifyElements_Delete");
        print("All elements of listNumber: ", *self._listNumber)
        # Specify value of element to be removed
        self._listNumber.remove(1) #Delete one element
        print("All elements of listNumber: ", *self._listNumber)
        # Specify index of element to be removed
        del self._listNumber[1]  #Delete one more element
        print("All elements of listNumber: ", *self._listNumber)
        del self._listNumber[1:3] #Delete two more elements (in slices)
        print("All elements of listNumber: ", *self._listNumber)

    # List Modify - Update
    def ModifyElements_Update(self):
        print("------ ModifyElements_Update");
        print("All elements of listNumber: ", *self._listNumber)
        # Update element at index 0
        self._listNumber[0]=100
        print("All elements of listNumber: ", *self._listNumber)
        # Update 3 elements directly using slices
        self._listNumber[1:3] = [200,300,400];
        print("All elements of listNumber: ", *self._listNumber)
        # Update 4 elements from another list using slides
        localList1 = [500, 600, 700, 800]
        self._listNumber[4:7] = localList1;
        print("All elements of listNumber: ", *self._listNumber)

# Instantiate the class and call member functions
dlc=DemoListClass()
dlc.Initialize()
dlc.PrintInit()
dlc.ModifyElements_Add()
dlc.ModifyElements_Delete()
dlc.ModifyElements_Update()
The output for the above code is:
------ PrintInit
String list element 0 :One
String list element 3 :Four
Number list element 3 :4
------ ModifyElements_Add
All elements of listNumber 1 2 3 4 5 6 7 8
All elements of listNumber:  1 2 3 4 5 6 7 8 9 10 11 12
All elements of listNumber:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
------ ModifyElements_Delete
All elements of listNumber:  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
All elements of listNumber:  2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
All elements of listNumber:  2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
All elements of listNumber:  2 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
------ ModifyElements_Update
All elements of listNumber:  2 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
All elements of listNumber:  100 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
All elements of listNumber:  100 200 300 400 8 9 10 11 12 13 14 15 16 17 18 19 20
All elements of listNumber:  100 200 300 400 500 600 700 800 11 12 13 14 15 16 17 18 19 20

In the next article we take a look at Python dictionaries.

Comments

Popular posts from this blog

Part III: Backpropagation mechanics for a Convolutional Neural Network

Introducing Convolution Neural Networks with a simple architecture

Deriving Pythagoras' theorem using Machine Learning