Python Lists

Python Lists

The List is a collection of heterogeneous data type that is ordered and changeable i.e. mutable. It allows duplicate members.

  • Creation of list:
    • Using square brackets:
    • # creation of empty list
      empty_list = []
      print(empty_list)
      
      mylist = ["apple","orange","kiwi"]
      print(mylist)
      

      Output:

      []
      ['apple', 'orange', 'kiwi']
      

    • Using list() built-in function:
    • # creation of empty list
      empty_list2 = list()
      print(empty_list2)
      
      # creation of list of characters
      mylist2 = list("apple")
      print(mylist2)
      

      Output:

      []
      ['a', 'p', 'p', 'l', 'e']
      

  • Access elements of the list:
  • We can access any element from the list by using its index. Indexing in lists starts from 0.

    mylist = ["apple","orange","kiwi"]
    
    # accessing 1st element
    print(mylist[0])
    
    # accessing 3rd element
    print(mylist[2])
    

    Output:

    apple
    kiwi
    

  • Accessing elements within a range or Slicing of a List:
  • We can specify a range of indexes by indicating where to begin and where to end the range. When indicating a range, the return value will be a new list with the specified elements.

    mylist = ["apple","orange","kiwi",
              "banana","mango"]
    
    # print element from 2nd 
    # index to 5th index
    print(mylist[2:5])
    

    Output:

    ['kiwi', 'banana', 'mango']
    

  • Add an element to the list:
    • Using append() method:
    • Elements are adding to a list from last position.

      mylist = ["apple","orange","kiwi"]
      
      # append element in the 
      # list from back side
      mylist.append("banana")
      
      print(mylist)
      

      Output:

      ['apple', 'orange', 'kiwi', 'banana']
      

    • Using insert() method:
    • The append() method adds only a single item at the end of the list. Whereas the insert() method is used to add an element at the desired position. This method needs two arguments i.e. (position, value).

      mylist = ["apple","orange","kiwi"]
      
      # insert element in the list
      # at 0th index
      mylist.insert(0,"mango")
      
      print(mylist)
      

      Output:

      ['mango', 'apple', 'orange', 'kiwi']
      

  • Remove an element from the list:
    • Using remove() method:
    • This method is used to remove an element from the list. You need to mention the element to be removed from the list. In case element is not exist in the list, this method gives error.

      mylist = ["apple","orange","kiwi",
                "banana"]
      
      # remove "kiwi" element 
      # from the given list
      mylist.remove("kiwi")
      
      print(mylist)
      

      Output:

      ['apple', 'orange', 'banana']
      

    • Using pop() method():
    • This method is by default removes the last element from the list. To remove a specific element from the list, specify its index as an argument to this method. if index is not in the range then it gives error.

      mylist =["apple","orange","kiwi",
               "banana"]
      
      # by default pop() method
      # remove last element 
      # from the list
      mylist.pop()
      
      print(mylist)
      
      mylist2 = ["apple","orange","kiwi","banana"]
      
      # remove 2nd index element
      # from the list
      mylist2.pop(2)
      
      print(mylist2)
      

      Output:

      ['apple', 'orange', 'kiwi']
      ['apple', 'orange', 'banana']
      

  • Negative Indexing:
  • Negative indexing refers to the beginning from the end, -1 means last element, -2 means second last element etc.

    mylist = ["apple","orange","kiwi"]
    
    # accessing last element 
    # form the list 
    print(mylist[-1])
    
    # accessing 3rd last element
    # form the list
    print(mylist[-3])
    

    Output:

    kiwi
    apple
    

  • Change the value of an element:
  • To change the value of an element, index number is used.

    mylist =["apple","orange","kiwi"]
    
    # overwrite 1st index element
    mylist[1] = "cherry"
    
    print(mylist)
    

    Output:

    ['apple', 'cherry', 'kiwi']
    

  • Iterate through list:
  • We can iterate through the list using for loop.

    mylist =["apple","orange","kiwi"]
    
    print("1st Way of for looping:", end=" ")
    # for loop
    for element in mylist :
        # accessing element
        print(element, end = " ")
        
    print("\n2nd Way of for looping:", end = " ")
    # for loop
    for i in range (0,3) :
        # access element through 
        # indexing
        print(mylist[i], end = " ")
    

    Output:

    1st Way of for looping: apple orange kiwi 
    2nd Way of for looping: apple orange kiwi 
    

  • Length of list:
  • To calculate the length of the list len() built – in function is used.

    mylist = ["apple","orange","kiwi",
              "banana","mango"]
    
    # length of the given list
    print(len(mylist))
    

    Output:

    5
    

  • Merge two list:
    • Using ‘+’ operator:
    • We can use the ‘+’ operator to join two or more lists.

      list1 = ["apple","banana"]
      list2 = ["cherry","orange"]
      
      # merging two lists
      mylist = list1 + list2
      
      print(mylist)
      

      Output:

       
      ['apple', 'banana', 'cherry', 'orange']
      

    • Using extend() method:
    • The extend() method is used to add multiple elements to the list simultaneously.

      list1 = ["apple","banana"]
      list2 = ["cherry","orange"]
      
      # mergin two lists
      list1.extend(list2)
      
      print(list1)
      

      Output:

      ['apple', 'banana', 'cherry', 'orange']
      

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds