Python

Constant dripping wears away a stone

Lists


Alist in Python is used to store a sequence of multiple items. The items of a list may be of the same or different data types, and they are enclosed with sqaure brackets and are separated with commas.

  • List Properties:
    • Ordered: Lists are ordered. The items in a list are in a defined order, and the order will not change.
    • Changable: Lists can be changed. You can add more items to a list, change the items in a list, or remove items from a list.
    • Allow Duplicates: Items in a list can be duplicates.
  • Access List Items:
    • Positive indexing: A list item can be accessed by referring to its index number in the list. The index of the first list item is 0, the index of the second item is 1, and so on. If the index is out of range, report theIndexErrorerror.
    • Negative indexing: Different from positive indexing, negative indexing begins from the end to the beginning. -1 refers to the last item, -2 refers to the second to the last item, and so on. If the index is out of range, report theIndexErrorerror.
    • Range indexing: Multiple items can be accessed by using a range of indices to specify where to start and where to end. For example, a_list[start:end] is a new list that includes all the items that are referred to bystart(inclusive) up toend(exclusive).
  • List Methods:
    • append(elem): Add the elementelemof any type at the end of the given list.
      • elem: Required. An element of any type to be appended to the list.
    • clear(): Remove all elements from the given list. The given list becomes empty.
    • copy(): Return a copy of the given list.
    • count(val): Return the number of elements with the specified valuevalin the given list.
      • val: Required. A value to be searched in the list.
    • extend(iter): Add the elements of the iterableiterat the end of the given list.
      • iter: Required. An iterable whose elements to be appended to the list.
    • index(val): Return the index of the first element that has the specified valuevalin the given list.
      • val: Required. A value to be searched for indexing in the list.
    • insert(pos, val): Insert an element with the specified valuevalto the given list at the specified positionpos. If the position is not in the range of the list indices, insert the element at the end of the list if the positionposis positive, or insert the element at the beginning of the list if the positionposis negative.
      • pos: Required. An index number at which an element is to be inserted to the list.
      • val: Required. A value. An element with such value is to be inserted to the list.
    • pop([pos]): Removes the element at the specified positionposfrom the given list ifpos is given; otherwise, remove the last element from the list. If the specified positionposis not in the range of the list indices, report theIndexErrorerror.
      • pos: Optional. An index number at which an element is to be removed from the list.
    • remove(val): Removes the first element with the specified value from the given list.
      • val: Required. A value. The first element with such value is to be removed from the list.
    • reverse(): Reverses the order of the list.
    • sort([reverse=False, key=func_name]): Sort the list in descending order using the sorting criteria specified by the functionfunc_name,ifreverseis True; otherwise, sort the list in ascending order.
      • reverse: Optional. True or False. If it is True, sort the list in descending order.
      • key: Optional. A function that specifies the sorting criteria.
  • Built-in Functions with List:
    • all(alist): Returns True if all elements in the listalistare true or the list is empty; otherwise, returns False.
      • alist: Required. A list.
    • any(alist): Returns True if any element in the list is true. If the list is empty or no element in the list is true, returns False.
    • enumerate(iter[, start]): Adds a counter that starts withstartto the iterableiter.and returns it as an enumerating object. Ifstartis not given, use 0 to start the counter.
      • iter: Required. An iterable.
      • start: Optional. A number from which the counter starts. By default, it is 0.
    • len(alist): Return the number of items in the listalist.
    • max(alist): Return the maximum element in the listalist.
    • min(alist): Return the minimum element in the listalist.
    • sum(iter[, start=0]): Adds all items of the iterableiterandstart and returns the sum. Ifstartis not given, use 0.
      • iter: Required. An iterable whose items should be numbers.
      • start: Optional. A number whose value is to be added to the sum of all items in the iterable. By default, it is 0.
    • sorted(iter[, key=func_name, reverse=False]): Sorts the listiterusing the sorting criteria specified by the functionfunc_nameand returns the sorted list. Ifreverseis True, sort the list in descending order; otherwise, sort the tuple in ascending order.
      • iter: Required. An iterable to be sorted.
      • key: Optional. A function that specifies the sorting criteria.
      • reverse: Optional. True or False. If it is True, sort the iterable in descending order.
  • Operators with List:
    • + (concatenation): It appends all items in the right operand list to the end of the left operand list and returns a new list. The two original lists are unchanged.
    • * (Repeating List Elements): Create a new list with the items in the given list repeated the specific number of times.