LIST METHODS
Once you create a list, Python will automatically give you additional functionality. Methods that are available with list object in Python programming are tabulated below.
They are accessed as listname.method(). This list is provided by www.programiz.com
Python List Methods | Description |
---|---|
append() | Add an element to the end of the list |
extend() | Add all elements of a list to the another list |
insert() | Insert an item at the defined index |
remove() | Removes an item from the list |
pop() | Removes and returns an element at the given index |
clear() | Removes all items from the list |
index() | Returns the index of the first matched item |
count() | Returns the count of number of items passed as an argument |
sort() | Sort items in a list in ascending order |
reverse() | Reverse the order of items in the list |
copy() | Returns a shallow copy of the list |
all() | Return True if all elements of the list are true (or if the list is empty) |
any() | Return True if any element of the list is true. If the list is empty, return False |
len() | Return the length (the number of items) in the list |
list() | Convert an iterable (tuple, string, set, dictionary) to a list |
max() | Return the largest item in the list |
min() | Return the smallest item in the list |
sorted() | Return a new sorted list (does not sort the list itself) |
sum() | Return the sum of all elements in the list |
Sample Code
Using the interpreter of your choice, Atom or othe one below, create a list with a minimum of 5 items (strings, integers, floats, etc…). Use the methods listed above and see the result that is created.
Check Your Understanding
Before moving to the next section, let’s see how well you know the current material.
- Quiz yourself on the methods listed above and see how many you can get right.