Jump Start With Python - Part 6 (Lists)

This article is a continuation of my previous articles of the series Jump Start with Python. To read, please refer to these links: 
    Introduction 

    The most basic data structure in Python is sequence. A sequence is an abstract data type that represents an ordered sequence of values (items), where the same value may occur more than once. Python has six built-in types of sequences but among them most common are lists and tuples.
     
    List is the most versatile data type in Python, It can also be termed as "Python's Workhorse Data Type". Lists in Python can be written as a list of comma-separated values (items) enclosed between square brackets irrespective of their type. The following is a simple example of lists in Python. 


    Creating a List 

    It is very easy to create a list in python, just enclose values between square brackets separated by commas.

    Syntax: ["item1", "item2", "item3", -------- , "itemn"]



    In Python, indexes for lists start with 0. 

    Sublists:

    A list in Python can have sublists as elements. These sublists may contain sublists as well. List can be recursively constructed by sublist structures. The following is an example for sublists in Python:
      


    Complexity of sublists can be increased by nesting sublists. 

    Accessing Lists

    Python allows accessing values of a list using list indices i.e. list element index which starts from 0. Multiple elements of list can be accessed using slicing range of elements from the list.
     
    The following is the example for accessing lists:


    Updating Lists:

    Python allows updating single or multiple elements in lists. The following are some examples depicting list update:



    Deleting Lists:

    Python allows deletion of lists directly as well as list element(s).
     
    Syntax: del list_name / del list_name[index] 



    Basic Operations in Lists

    List responds to almost all basic operations and some of them are discussed as below:

    Length Operation: 

    This operation is used to determine length of list. It consists of one parameter and it is explained as below:

    1. list_name: It is name of list whose length is to be determined.
     
    Syntax: len(list_name)



    Concatenation Operation

    This operation is used to concatenate two or more than two lists.
     
    Syntax: list1 + list2 + ----- + listn 


    Repetition Operation

    This operation is used to create multiple duplicate list elements simultaneously.
     
    Syntax: list_name * num

    list_name is name of list and num is number of repetitions.

     

    Membership Operation

    This operation is used to check membership of input element with list or list elements.
     
    Syntax: input in list_name

    input is input element and list_name is name of list in which membership will be checked. If membership exists it will return true else it will return false.


    Iteration Operation

    This operation is used to iterate elements of a list and print them.
     
    Syntax: for x in list_name: print(x)
     
    list_name is the name of list to be iterated.


    List Methods & Functions

    Maximum Method

    This method is used to determine maximum element in a list. It consists of one parameter and it is explained as below:

    1. list_name: It is name of list to be checked.
     
    Syntax: max(list_name)

     
    Minimum Method

    This method is used to determine minimum element in a list. It consists of one parameter and it is explained as below:

    1. list_name: It is name of list to be checked.
     
    Syntax: min(list_name)



    Convert to List Method

    This method is used to convert a sequence/tuple to a list. It consists of one parameter and it is explained as below:

    1. seq: It is sequence/tuple to be converted into list specified as input.
     
    Syntax: list(seq)



    Append Method

    This method is used to append or add an element to a list. It consists of one parameter and it is explained as below:

    1. obj: It is element to be added to the list.
     
    Syntax: list_name.append(obj)



    Count Method

    This method is used to count the number of occurrences of an element in the list. It consists of one parameter and it is explained as below:

    1. obj: It is element to be checked in the list.
     
    Syntax: list_name.count(obj)



    Extend Method

    This method is used to extend existing list with sequence list provided as input. It consists of one parameter and it is explained as below:

    1. seq: It is sequence to be appended with existing list.
     
    Syntax: list_name.extend(seq)



    Object Index Method

    This method is used to get lowest index of object specified as input. it consists of one parameter and it is explained as below:

    1. obj: It is element whose index is to be retrieved.
     
    Syntax: list_name.index(obj)



    Index Insert Method

    This method is used to insert an element in list at index specified as input. It consists of two parameters and they are explained as below:

    1. index: It is index position of object specified as input.
    2. obj: It is object to be inserted in the list.
     
    Syntax: list_name.insert(index, obj)



    Remove Method

    This method is used to remove element specified as input from the list. it consists of one parameter and it is explained as below:

    1. obj: It is element to be removed from the list.
     
    Syntax: list_name.remove(obj)



    Pop Method

    This method is used to remove and return last index element of the list. It consists of one parameter and it is explained as below:

    1. obj: It is index of element to be removed from the list. By default it is last index element of list.
     
    Syntax: list_name.pop(obj)



    Sort Method

    This method is used to sort elements in the list. It consists of one parameter and it is explained as below:

    1. func: It is sorting function, and is optional to specify.
     
    Syntax: list_name.sort([func])



    Reverse Method

    This method is used to reverse the elements in a list.
     
    Syntax: list_name.reverse()

      

    Up Next
      Ebook Download
      View all
      Learn
      View all