Built-in List Functions & Methods

Built-in List Functions & Methods

Built-in List Functions & Methods

Built-in List Functions & Methods: 

Python has several built-in methods and functions that can be used with lists. Here are some of the most commonly used ones:

Methods:

  • append(x) – adds an item to the end of the list
  • extend(iterable) – adds all the items from an iterable (e.g. another list) to the end of the list
  • insert(i, x) – inserts an item at a specific index in the list
  • remove(x) – removes the first occurrence of an item from the list
  • pop([i]) – removes and returns the item at a specific index (or the last item if no index is provided)
  • index(x) – returns the index of the first occurrence of an item in the list
  • count(x) – returns the number of times an item appears in the list
  • sort() – sorts the items in the list in ascending order (or using a custom key function)
  • reverse() – reverses the order of the items in the list

         

 Functions:                          

  • len(iterable) – returns the number of items in an iterable (including lists)
  • sorted(iterable) – returns a new sorted list from the items in an iterable (without modifying the original iterable)
  • sum(iterable) – returns the sum of all the items in an iterable (assuming they can be added together)
  • max(iterable) – returns the maximum item in an iterable (assuming they can be compared)
  • min(iterable) – returns the minimum item in an iterable (assuming they can be compared)

     

These are just a few examples of the many built-in methods and functions that can be used with lists in Python.

Join To Get Our Newsletter
Spread the love