List/Array Methods

What You'll Learn: In this tutorial, you'll discover various built-in methods you can use to work with lists in Python. These methods allow you to manipulate, analyze, and organize list items efficiently.

List Methods and Descriptions

  1. append()
    • Description: Adds an element at the end of the list.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      thislist.append("orange")
      print(thislist)
      
    • Output:

       
      ['apple', 'banana', 'cherry', 'orange']
      
  2. clear()
    • Description: Removes all the elements from the list.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      thislist.clear()
      print(thislist)
      
    • Output:

       
      []
      
  3. copy()
    • Description: Returns a copy of the list.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      mylist = thislist.copy()
      print(mylist)
      
    • Output:

       
      ['apple', 'banana', 'cherry']
      
  4. count()
    • Description: Returns the number of elements with the specified value.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry", "banana"]
      print(thislist.count("banana"))
      
    • Output:

       
      2
      
  5. extend()
    • Description: Adds the elements of a list (or any iterable) to the end of the current list.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      tropical = ["mango", "pineapple", "papaya"]
      thislist.extend(tropical)
      print(thislist)
      
    • Output:

       
      ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
      
  6. index()
    • Description: Returns the index of the first element with the specified value.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      print(thislist.index("cherry"))
      
    • Output:

       
      2
      
  7. insert()
    • Description: Adds an element at the specified position.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      thislist.insert(1, "orange")
      print(thislist)
      
    • Output:

       
      ['apple', 'orange', 'banana', 'cherry']
      
  8. pop()
    • Description: Removes the element at the specified position.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      thislist.pop(1)
      print(thislist)
      
    • Output:

       
      ['apple', 'cherry']
      
  9. remove()
    • Description: Removes the item with the specified value.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      thislist.remove("banana")
      print(thislist)
      
    • Output:

       
      ['apple', 'cherry']
      
  10. reverse()
    • Description: Reverses the order of the list.
    • Example Code:

      python
      thislist = ["apple", "banana", "cherry"]
      thislist.reverse()
      print(thislist)
      
    • Output:

       
      ['cherry', 'banana', 'apple']
      
  11. sort()
    • Description: Sorts the list.
    • Example Code:

      python
      thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
      thislist.sort()
      print(thislist)
      
    • Output:

       
      ['banana', 'kiwi', 'mango', 'orange', 'pineapple']
      

Try It Yourself: Fun Exercises

  1. Organize Your Playlist:
    • Create a list of your favorite songs.
    • Use sort() to arrange them alphabetically.
  2. Manage Your Shopping List:
    • Create a shopping list.
    • Use append(), remove(), and reverse() to manage your list.

Summary:

In this Python tutorial, we learned about various list methods including append(), clear(), copy(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). These methods help you manage and manipulate list items efficiently. Keep experimenting and have fun with lists in Python!