Python list methods are built-in functions that allow you to modify, search, and manipulate lists directly. These methods operate on the list object itself and typically modify it in place.
Core List Methods
append(item): Adds a single item to the end of the list.extend(iterable): Adds all elements from another list or iterable to the end of the current list.insert(index, item): Inserts an item at a specified position in the list.remove(item): Removes the first occurrence of a specified item from the list (raisesValueErrorif not found).pop(index): Removes and returns the item at the given index (or the last item if no index is specified).clear(): Removes all items from the list.index(item): Returns the index of the first occurrence of the specified item (raisesValueErrorif not found).count(item): Returns the number of times an item appears in the list.sort(key=None, reverse=False): Sorts the list in ascending order by default; usereverse=Truefor descending order.reverse(): Reverses the order of elements in the list in place.copy(): Returns a shallow copy of the list (useful to avoid unintended mutations).
Key Notes
Most list methods modify the original list and do not return a new list.
For non-destructive operations, use the built-in
sorted()function (returns a new sorted list) orreversed()(returns an iterator).Use
list.copy()to create a shallow copy when you need to preserve the original list.The
delkeyword can also be used to remove items or slices, but it is not a method.
For detailed examples and practice, refer to resources like W3Schools or Learn By Example.
Creating a list of methods to be executed in Python - Stack Overflow
How do I mock boto3 method calls when they're called from within a custom class' custom method?
List of all Python dunder methods?
Sounds like a classic case of X-Y-problem. What are you trying to achieve?
I don't think trying to maintain a comprehensive list of dunder methods is a good idea. Consider that this list is bound to change with upcoming versions of Python and has already undergone considerable changes in the past. You may end up maintaining varying lists of magic methods for different versions of Python.
I think the goto approach here would be to just try to call the respective method from within a generic __getattribute__ and react appropriately to TypeError.
It may also be possible to analyse the proxied object's class dict (someobject.__class__.__dict__) to find out which dunder methods were defined on that class. Of course you may end up having to walk up the inheritance chain as well, so this is bound to get quite complicated.
What is Python's list.append() method WORST Time Complexity? It can't be O(1), right?
Videos
You could create a list of the method names then use getattr() to access the methods:
instructions = ["shrink", "grow", "shrink"]
for i in instructions:
getattr(elephant, i)()
As an alternative to using strings for your list of instructions, you could do the following:
instructions = [someClass.shrink, someClass.grow, someClass.shrink,
someClass.shrink, someClass.grow, someClass.invert]
elephant = someClass(90)
sizeList = []
for ins in instructions:
ins(elephant)
sizeList.append(elephant.size)