Python Lists Methods

Python Lists Methods

This article is all about Python List Methods

ยท

2 min read

  1. append()

    • Adds an element at the end of the list

      company = ['apple', 'microsoft', 'adobe']
      company.append('oneplus')
      print(company)
      
  2. clear()

    • Removes all the elements from the list

      fruits = ['apple', 'banana', 'cherry', 'orange']
      fruits.clear()
      print(fruits)
      
  3. copy()

    • Returns a copy of the list

      fruits = ['apple', 'banana', 'cherry', 'orange']
      x = fruits.copy()
      print(x)
      
  4. count()

    • Returns the number of elements with the specified value

      fruits = ['apple', 'banana', 'cherry', 'orange']
      x = fruits.count("cherry")
      print(x)
      
  5. extend()

    • Add the elements of a list (or any iterable), to the end of the current list

      fruits = ['apple', 'banana', 'cherry', 'orange']
      cars = ['Ford', 'BMW', 'Volvo']
      fruits.extend(cars)
      print(fruits)
      
  6. index()

    • Returns the index of the first element with the specified value

      fruits = ['apple', 'banana', 'cherry', 'orange']
      x = fruits.index("cherry")
      print(x)
      
  7. insert()

    • Adds an element at the specified position

      fruits = ['apple', 'banana', 'cherry', 'orange']
      fruits.insert(1, "mango")
      print(fruits)
      
  8. pop()

    • Removes the element at the specified position

      fruits = ['apple', 'banana', 'cherry', 'orange']
      fruits.pop(1)
      print(fruits)
      
  9. remove()

    • Removes the first item with the specified value

      fruits = ['apple', 'banana', 'cherry', 'orange']
      fruits.remove("banana")
      print(fruits)
      
  10. reverse()

    • Reverses the order of the list
    fruits = ['apple', 'banana', 'cherry', 'orange']
    fruits.reverse()
    print(fruits)
    
  11. sort()

    • Sorts the list
      fruits = ['apple', 'banana', 'cherry', 'orange']
      fruits.sort()
      print(fruits)
      

Did you find this article valuable?

Support BigSmoke's Blog by becoming a sponsor. Any amount is appreciated!

ย