Python List Methods. A summary of the list methods Python... | by Robert Alterman | Towards Data Science

In Python, a list is a collection of pieces of data. A list is surrounded by square brackets [ ] with each item separated by a comma ( , ), and can contain anywhere from zero to infinity items (or however many your computer will allow). Strings, numbers, booleans, even other lists can be items in a list. Lists are ordered and mutable (changeable), meaning each item is assigned to a specific index and can be sorted, and has the ability to be altered. Once you have a list, you are able to manipulate that list using what’s known as a ‘method.’ Like string methods, to use a list method, you simply write the list followed by .[method](). For example, to run the append() method on the list [‘pepperoni’, ‘sausage’, ‘mushroom’], you would just write [‘pepperoni’, ‘sausage’, ‘mushroom’].append(‘onion’); if that list were set to a variable, you would do that variable.append(‘onion’). As you see with the append() example, some methods accept what are known as ‘arguments,’ which go in the parenthesis and further define what the method will do. The Python language has a lot of built-in methods, like append(), that allow you to easily alter lists.
The append() method allows you to add another item to the end of your list. The method takes one required argument, which is the item you wish to add to your list.
Syntax: list.append(item)toppings = ['pepperoni', 'sausage', 'mushroom']toppings.append('onion') --> ['pepperoni', 'sausage', 'mushroom', 'onion']
The extend() method is similar to append() in that it allows you to add onto your list; however, the extend() method allows you to add all of the items from another iterable (list, tuple, set, etc.) to the end of your list as separate items instead of one item. The method takes one required argument, the iterable.
Syntax: list.extend(iterable)toppings = ['pepperoni', 'sausage', 'mushroom']more_toppings = ['onion', 'bacon']toppings.extend(more_toppings)--> ['pepperoni', 'sausage', 'mushroom', 'onion', 'bacon']To contrast...toppings.append(more_toppings)--> ['pepperoni', 'sausage', 'mushroom', ['onion', 'bacon']]
As you can see above, when the extend() method is used with an iterable, each item in the iterable is added to the list as separate items no longer bounded. On the contrary, when the append() method is used with an iterable as the argument, the entire iterable is added to the list as one item. It’s always important to pay close attention to the commas and brackets present in a list.
The pop() method allows you to remove an element from your list at a specified index value. The method can take one optional argument, the integer value of the index you wish to remove — by default, pop() will remove the last item in the list, as the default value is -1.
Syntax: list.pop(index)toppings = ['pepperoni', 'sausage', 'mushroom']toppings.pop(1)--> ['pepperoni', 'mushroom']toppings.pop()--> ['pepperoni', 'sausage']If you wanted to retrieve the removed item...extra = toppings.pop(1)extra --> 'sausage'
Like the pop() method, the remove() method allows you to remove an item from your list. The remove() method, though, removes the first occurrence of a specified value in a list. The method takes one required argument, the item you wish to remove.
Syntax: list.remove(item)toppings = ['pepperoni', 'sausage', 'mushroom']toppings.remove('sausage')--> ['pepperoni', 'mushroom']
For both pop() and remove(), if the argument is out of range or does not exist in the list, respectively, you will get an error.
The sort() method sorts a list by certain criteria. The method can take two optional arguments. The first argument is setting either reverse=True or reverse=False. By default, this argument is set to reverse=False, which will result in alphabetical order if the list consists of only strings, or ascending order if the list consists of only numbers. The second argument allows you to set a key= to a function that you can use to specify how exactly you want your list sorted if it’s more complex than the default ordering that the sort() method does. This could be a built-in Python function, a function you defined elsewhere in your program, or an in-line lambda function that you write.
Syntax: list.sort(reverse=True|False, key=function)toppings = ['pepperoni', 'sausage', 'mushroom']toppings.sort()--> ['mushroom', 'pepperoni', 'sausage']toppings.sort(reverse=True)--> ['sausage', 'pepperoni', 'mushroom']toppings.sort(reverse=True, key=lambda x: len(x))--> ['pepperoni', 'mushroom', 'sausage']* Sorted in reverse order by length of the topping nameprices = [1.50, 2.00, 0.50]prices.sort(reverse=False)--> [0.50, 1.50, 2.00]prices.sort(reverse=True)--> [2.00, 1.50, 0.50]pies = [['bacon', 'ranch'], ['sausage', 'peppers']]pies.sort(reverse=True)--> [['sausage', 'peppers'], ['bacon', 'ranch']]* Sorts iterators by their first value
The reverse() method simply reverses the order of the items in the list. The method takes no arguments.
Syntax: list.reverse()toppings = ['pepperoni', 'sausage', 'mushroom']toppings.reverse()--> ['mushroom', 'sausage', 'pepperoni']prices = [1.50, 2.00, 0.50]prices.reverse()--> [0.50, 2.00, 1.50]
The count() method returns the number of occurrences of a specified item in a list. The method takes one required argument, which is the item you wish to find the count of. This method can be useful if you wish to find out what items appear more than once in a list.
Syntax: list.count(item)toppings = ['pepperoni', 'sausage', 'mushroom', 'sausage']toppings.count('sausage')--> 2toppings.count('pepperoni')--> 1toppings.count('bacon')--> 0
The index() method returns the index of the first occurrence of the specified item. The method takes one required argument, which is the item whose index you wish to find. If the item does not exist in the list, you will get an error.
Syntax: list.index(item)toppings = ['pepperoni', 'sausage', 'mushroom', 'sausage']toppings.index('mushroom')--> 2toppings.index('pepperoni')--> 0
The insert() method inserts a specified item into a list at a specified index. The method takes two required arguments — the integer index you wish to insert the value at and the item you’d like to insert.
Syntax: list.insert(index, index)toppings = ['pepperoni', 'sausage', 'mushroom']toppings.insert(1, 'onion')--> ['pepperoni', 'onion', 'sausage', 'mushroom']
The copy() method simply returns a copy of your list. The method takes no arguments.
Syntax: list.copy()toppings = ['pepperoni', 'sausage', 'mushroom']toppings2 = toppings.copy()toppings2 --> ['pepperoni', 'sausage', 'mushroom']
The clear() method simply removes all items from a list, leaving an empty list. The method takes no arguments.
Syntax: list.clear()toppings = ['pepperoni', 'sausage', 'mushroom']toppings.clear()--> []
And that’s that. With these aforementioned methods, you should be set to perform the operations you will need on Python lists. For more information and examples, make sure to check out the w3schools list.
Happy coding and be sure to check out my blog on Python string methods below!