Signup/Sign In
LAST UPDATED: SEPTEMBER 15, 2021

Python Lists: Different ways of Adding and Removing Elements

    The list is one of the most important data structures in Python that is widely used in many applications. It is a mutable data structure, i.e after it has been created various operations (including appending, deleting, replacing elements) can be performed on it. It is 0 indexed, like many other data structures, meaning the first element is stored at index 0.

    How do Python lists work?

    Once a list has been created (let's say my_first_list), it is allocated memory space and the address of this memory space is stored in the variable my_first_list.

    So literally speaking, my_first_list doesn't actually contain the list elements, but just a reference to the memory where it is stored.

    Suppose you wish to copy the contents from one list to another, just equating them using the = symbol doesn't copy the contents from one list to the other. It merely makes both the lists and points them to one memory location.

    Consider my_first_list to be our original list and my_first_list_copy to be the list wherein the contents from my_first_list need to be copied.

    
    # point to memory space where elements are stored
    my_first_list_copy = my_first_list 
    
    

    On the other hand, to actually copy the contents, use the code below:

    
    my_first_list_copy = my_first_list.copy()
    
    # or another way is...
    
    my_first_list_copy = my_first_list.copy[:]
    
    

    This is known as list slicing. More about this here: List slicing in Python.

    How to create a Python list?

    Lists can be created in many ways and they have been listed below:

    
    my_first_list = []
    
    my_first_list = list()
    
    

    The above 2 lines create an empty list by the name my_first_list.

    Adding elements to a Python list

    In python, a list can hold multiple data types together. In the code below, it can be observed that a list can store elements of type int, double/float, string, etc together or any other datatype.

    
    my_first_list = [1,2,3, 'Studytonight', 1.23]
    

    There are many different ways of adding data to a list in python. Some of those are listed below:

    1) Using the append() function

    The append function adds a single element to the end of the list. It can be used to add a data element of any type to an existing list. We can even add list to another list using this function.

    
    my_first_list.append('Python')
    my_first_list.append([0,0,7])
    print(my_first_list)
    
    

    Output:

    [1,2,3, 'Studytonight', 1.23,'Python',[0,0,7]]

    2) Using the + Operator

    The + operator behaves like a concatenation operator and adds the element to the end of an existing list.

    
    my_first_list + ['Java']
    print(my_first_list)
    
    

    Output:

    [1,2,3, 'Studytonight', 1.23,'Python',[0,0,7],'Java']

    3) Using the extend() function

    The extend function is used to add elements present in an iterable object to a list as separate entities, i.e if we have a tuple (10,11) then this function will add 10 and 11 as separate elements in the calling list. We can use this function with a list or tuple or any other iterable to add multiple elements to a list using a single line of code.

    
    my_first_list.extend([70,90])
    print(my_first_list)
    
    

    Output:

    [1,2,3, 'Studytonight', 1.23,'Python',[0,0,7],'Java',70,90]

    4) Using insert() function

    The insert function takes into account the index where the element needs to be added and the element value.

    
    my_first_list.insert(0, 'Hello')
    print(my_first_list)
    
    

    Removing elements from Python list

    Removing an element is super easy from a list in python. We can use the remove() method or the del function or the pop() function to delete any element using either its value or its index.

    
    # using remove function
    my_first_list.remove(3)
    
    # using the del keyword
    del my_first_list[1]
    
    # using the pop function
    my_first_list.pop(2)
    
    
    1. While using the remove function, the element value in the list is given. This value can be present at any location within the list.

    2. While using the del keyword, the list element's index has to be provided.

    3. Pop function's default argument is -1, which means it removes the last element of the list by default(if no argument is provided). In other cases, the index of the element can be specified as a parameter to the pop function.

    Conclusion

    In this article we have learned a lot about python list starting from how to create a list, adding new elements to a list to removing elements in list. After this, we would suggest you to write simple python scripts to practice using python list as this is one of the most widely used data type in python.

    You may also like:

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS