List Methods and Useful Built-in Functions
• An IndexError exception is raised if an invalid index is used
• len function: returns the length of a sequence such as a list
• Example: size = len(my_list)
• Returns the number of elements in the list, so the index of last
element is len(list)-1
• Can be used to prevent an IndexError exception when
iterating over a list with a loop
• You can use the in operator to determine whether an item is contained in a list
• General format: item in list
• Returns True if the item is in the list, or False if it is not in the list
• Similarly you can use the not in operator to determine whether an item is not in a list
• append(item): used to add items to a list – item is appended to the end of the existing list
• index(item): used to determine where an item is located in a list
• Returns the index of the first element in the list containing item
• Raises ValueError exception if item not in the list
• insert(index,item): used to insert item at position index in the list
• sort(): used to sort the elements of the list in ascending order
• remove(item): removes the first occurrence of item in the list
• reverse(): reverses the order of the elements in the list
• del statement: removes an element from a specific index in a list
• General format: del list[i]
• min and max functions: built-in functions that returns the item
that has the lowest or highest value in a sequence
• The sequence is passed as an argument