Lists in Python

What is a List?
  • The list is one of the most common collection datatypes in Python which is ordered and mutable (changeable).
  • It is used to store a group of an element together in a sequence.
  • A list can store both homogenous and heterogeneous elements i.e., a single list may contain data types like integers, floats, strings as well as objects.
    • Individual value in a list is called an element.
    • These elements will be stored in a contiguous memory location.
      • For Ex:
        • Number1 = 10
        • Number2 = 20
        • Number3 = 30
        • Number4 = 40 
    • These individual variables can be represented in a list as
      • Number_list = [ 10, 20, 30, 40 ]
    Creating a List:
    • In Python, a List can be created by placing all the elements (items) inside square brackets [ ], separated by commas.
    • Empty List can also be created without placing any elements inside square brackets [ ].
    Creating a List in Python
    • From the above example, it is clear that the Python list can have elements of the same data type or different data types.
    • We can also use the list() constructor to create a new list.
    list() constructor
    Accessing a List:

    1) Indexing:
    • In Python, each element (value) in a list has a position known as an index.
    • These index number always starts from 0 and it cannot be a float number or any other type, this will result in TypeError.
    • For Ex: A-List and its corresponding index position
      • Number_list = [ 10, 20, 30, 40 ]

    • Here, the List is having 4 elements, so its index position starts from 0 to 3.
    • These index positions (Number_list[index]) help to directly access an element (value) from the list.
    Accessing a List using Index position
    • Similarly, We can't access value beyond the total number of elements in that list. That will result in an IndexError.
    Index out of range error
    2) Negative Indexing:
    • In Python, Negative Indexing allows accessing the list element starting from the end of the list.
    • Index number -1 refers to the last item, -2 refers to the second last, and so on.
    Negative index

    Negative Index Accessing
    3) Slicing a List:
    • For accessing a particular range of elements (values) in a list, we can use the slicing method (slicing operator ':' ).
    • We need to specify the range of indexes [start index: end index], which will return a new list for the specified range.
    • Syntax: list_name [ Start index : End index ]
    • The start index will be included but the end index will not be included while searching a range of items.
    • If we left the start index empty, then the range will start from the first. Similarly, if we left the end index empty, then the range will consider the end of the list. 
    List Slicing
    • Similarly, we can specify a negative index to start the search from the end of the list.
    List Slicing - Negative Index

    Iterating List elements:
    • As elements are stored sequentially in the list, we can use a for loop to iterate the elements.
    List iteration using for loop
    • To determine, whether a specific value is present in the list. we can use the 'in' keyword.
    Checking if an item exists using the 'in' keyword

    Changing List elements:
    • Since List is mutable, we change the elements (values) by normal assignment operation using their index positions.
    • We can also change the range of elements using their start, end index positions.
    Changing List elements
     Adding a new element to the List:

    1) append():
    • We can use append() method to add an element at the end of the list.
    2) extend():
    • To add more than one element to a list, we can use extend() method. 
    • Those elements should be passed as a list to the extend method arguments.
    3) insert():
    • To insert an element to the desired index location, we can use insert() method by passing the desired index and value.
    • Syntax: insert(index, value) 
    Adding new elements to the list
    Delete/Remove elements from the List

    1) remove()
    • Using remove() method, we can remove a particular element in the list by passing the element as a remove method argument.
    • syntax: remove(value)
    2) pop()
    • pop() method used to remove an element from the list by passing its index position.
    • syntax: pop(index)
    • If the index is not specified, then pop() method will remove the last element from the list and returns the removed element.
    3) Del
    • We can delete one or more elements from the list, using Del method.
    • Del method can also delete the entire list.
    4) clear()

  • To empty a List, clear() method is used.

  • Delete/Removing elements from the List
    Concatenation/Joining and nesting of two Lists
    Concatenation and Nested List
    List Methods:
    • In Python, we have a set of built-in methods that you can use on Lists
    Adds an element at the end of the list
    Removes all the elements from the list
    Returns a copy of the list
    Returns the number of elements with the specified value
    Add the elements of a list to the end of the current list
    Returns the index of the first element with the specified value
    Adds an element at the specified position
    Removes the element at the specified position
    Removes the item with the specified value
    Reverses the order of the list
    Sorts the list

    No comments:

    Post a Comment

    You might also like

    Deploy your Django web app to Azure Web App using App Service - F1 free plan

    In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...