Basics of Python Lists.

Lists are one of the important python data structure widely used for solving numerical, statistical problems. Lists are more like arrays in other programming languages, But lists are a heterogeneous data structure, List is defined with items separated by the comma  (“,” ) and enclosed by square brackets (‘[]‘)

This below example using Jupyter notebook shows creating python list.  In Jupyter notebook, the output of each line is given immediately after the line.

Creating List in Python:

In the given example we are creating list_ex which is a list of numbers, other list list2 which is a list of alphabets, the other list list_hetro is a heterogeneous list which contains numbers, alphabets, and strings as list items. Python len() function is used to get the length of the list which indicates no of elements present in the list.

 

Accessing elements of lists and update List values in Python:

List elements in the python lists can be accessed using the index. List index starts from zero and the index of the final element of the list is N-1 (N = length of the list).

list_ex[0]  gives the first element of the list.

list_hetro[3] gives the 4th element of the list which is “concept”.

Similarly to update the values in this list we can use indexing for updating.

list_hetro[3] = “c2plabs.com”  This statement updates 4th element of list to “c2plabs.com”. Updated list can be seen by printing the list.

Deleting list element:

List elements can be deleted using the index, an example to delete 5th Element list_hetro

del list_hetro[5]    # This statement deletes element “prototype” from the list.

Slice Operator [:] and Slicing of List in Python:

Slice Operator [:] is used to extract a single element or ranges of elements from the list.

The syntax of slice operator is list_name [start_index : end_index]

Ex:  new_list=list_hetro [1:3]  #In this statement start index is 1 and end index is 3, in slicing operation end index is excluded.

Above statement create new list form list_hetro element from index “1” to index “2” excluding end index which is 3 here.

To get the final or last element of the list end index is “-1”.  Given example shows how to use slice operator to access list items.

Python Built-in functions:

Python offers built-in functions to work on lists.

Len (list) : Length of the list, No of elements present in the list.

Max(list) : This function returns maximum value present in the list.

Min(list) : This function return minimum value present in the list.

List methods:

Python lists have methods to perform different operations on the list like

list.append (object) : This method appends new object to list

list.count (object): This method returns the count of  occurrence of the object in the given list

list.remove(object): This method removes the first occurrence of the object from the list

list.insert (index, object): This method inserts the object at a index specified.

In the below given example we are defining list_new as

list_new = [“c2plabs.com”, “concept”, 2, “prototype”]

New elements “Machine”, “Learning”, “c2plabs.com” are added to list using append method.

This example also shows counting no of occurrences of “c2plabs.com” using list.count() method.

This example also shows the usage of list methods like list.remove() and list.insert.

Leave a Reply

Your email address will not be published. Required fields are marked *