Python List

    In this tutorial, we will discuss one of the most important Python datatypes. We will learn how to create, slice, add and remove a list.

    So let's discuss Python List.

    Table of Content:

    • How to create a list
    • How to access the elements from a list
    • How to slice a list in Python
    • How to add elements to a list
    • How to remove an element from a list
    • List methods
    • List operation in Python

    What is a List? How to create a list in Python?

    A list is a mutable collection of different Python data types , and to create a list, we place all those different elements inside a square bracket [] and separate them with a comma (,).

    Let’s create a list:

    list_1 = [1,2,3,4,5,6,7]
    list_2 = [1,"hello", "world", None, True]

    we can even place a list inside a list

    list_3 = [1,2,[3,4],"Hello"]

    Access elements from a list

    To access an element or value stored in a list can be accessed through indexing. Though when we initialize values inside the brackets [], and create the list, all those values are stored in contiguous memory locations, and an index number is associated with every value or element inside a list.

    So indexing is a method of retrieving values from a list using the integers values. Conventional indexing starts with 0 and ends with a number less than the total number of elements inside a list. It means that the 0 index is associated with the first element of the list. If we try to access an element of a list that does not have a corresponding index number, then it will throw an Index error.

    Syntax of accessing an element from a list

    List_name [ index_number]

    Let’s understand it with an example.

    list_1 = [1,2,3,4,[6,7,8],"hello"]


    [6, 7, 8]

    Negative Indexing:

    In Python, we can use a negative integer as indexing here -1 refers to the last element of the list, and so on.

    Let’s understand it with an example:

    list_1 = [1,2,"hello",None]



    Python List Slicing:

    In Python, you can control the sequence of elements and can access it. In list slicing, we can access a range of elements using indexing and colon.

    Syntax of list slicing:

    list_name[starting_index : ending_index]

    Let’s understand it with an example:

    list_1 = ["a","b","c","d","e","f","g"]
    print(list_1 [2 : 5])


    ['c', 'd', 'e']
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']

    Change or add elements to a list:

    As we have defined that a list is a mutable collection by mutable mean that the values of the list can be altered with the assignment operator (=). And we can also add a new element in a predefined list using a method (a function) known as append(). append() add a new element at the last of the position of the list.

    Syntax to perform append() method on a list


    Let’s understand it with an example:

    my_list =["a",1,"b",2,"d",3]
    my_list[4] = "c" #Here value d will change with c


    ['a', 1, 'b', 2, 'c', 3, 'd', 4]

    + and * operator on a list

    We use the + operator to concatenate 2 list and * operator to create a multiple copies of items present in the list.

    Syntax of using + and * operator on a list:

    list_1 + list_2
    list_1 * any_natural_number

    Let’s understand it with an example:

    list_1 =[1,2,3,4]
    list_2 =["a","b","c","d"]
    list_3 = list_1 + list_2
    print(list_3 *3 )


    [1, 2, 3, 4, 'a', 'b', 'c', 'd']
    [1, 2, 3, 4, 'a', 'b', 'c', 'd', 1, 2, 3, 4, 'a', 'b', 'c', 'd', 1, 2, 3, 4, 'a', 'b', 'c', 'd']

    To delete elements from a list

    There is keyword known as del which we can use to delete the entire list or a specific element.

    Let’s understand it with an example

    list_2 =["a","b","c","d"]
    del (list_2[2])


    ['a', 'b']

    Apart from del keyword, there are many other methods associated with the list that helps in delete elements from a list such as pop() and remove().

    List Methods :

    In python, the list has some methods and these methods are:

    append() - Add an element to the end of the list
    extend() - Add all elements of a list to the another list
    insert() - Insert an item at the defined index
    remove() - Removes an item from the list
    pop() - Removes and returns an element at the given index
    clear() - Removes all items from the list
    index() - Returns the index of the first matched item
    count() - Returns the count of the number of items passed as an argument
    sort() - Sort items in a list in ascending order
    reverse() - Reverse the order of items in the list
    copy() - Returns a shallow copy of the list. 

    List Comprehension:

    A list comprehension a shorthand technique of creating a list using a for loop statement

    Syntax of creating a list using list comprehension

    list_name = [items for statement on items]

    Let’s understand it with an example

    list_1 = [x**2 for x in range(10)]


    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

    In keyword with a list:

    If we want to know whether an item is presented in the list or not to do so we use the in keyword with the list. If the item is present in the list, we get a Boolean value True if not we get False.

    Let’s understand it with an example

    my_list = [1,2,"a","b"]
    print( "a" in my_list)
    print( "c" in my_list)



    List as an Iterable:

    As we know that list is iterable, like string so we can also use a list with the for-loop statement so the for loop can iterate through it.

    For example:

    for i in [1,2,3,4,5]: