This is a simple implementation of ArrayList in Python, full-featured and easy to use with more than 20 methods.
add- Add an element to the end of the listadd_at- Add an element at a specific indexremove- Remove an element from the listremove_at- Remove an element at a specific indexget- Get an element at a specific indexset- Set an element at a specific indexsize- Get the size of the listis_empty- Check if the list is emptycontains- Check if the list contains an elementindex_of- Get the index of an elementclear- Clear the listto_string- Get a string representation of the list
And a couple of magic methods:
__str__- Get a string representation of the list__repr__- Get a string representation of the list__len__- Get the size of the list__iter__- Get an iterator for the list__getitem__- Get an element at a specific index__setitem__- Set an element at a specific index__delitem__- Remove an element at a specific index__add__- Add two lists together__iadd__- Add two lists together__sub__- Subtract two lists__isub__- Subtract two lists__mul__- Multiply a list by a number__imul__- Multiply a list by a number__rmul__- Multiply a list by a number__eq__- Check if two lists are equal__ne__- Check if two lists are not equal__lt__- Check if one list is less than another__le__- Check if one list is less than or equal to another__gt__- Check if one list is greater than another__ge__- Check if one list is greater than or equal to another
from ArrayList import ArrayList # Create a new list list = ArrayList(10) # Add items to the list list.add(1) list.add(2) list.add(3) list.add(4) list.add(5) # Print the list print(list) # Remove an item from the list list.remove(3) # Print the list print(list) # Check if an item is in the list print(list.search(3)) print(list.search(4)) # Get an item from the list print(list.get(2)) # Print the length of the list print(len(list)) # Print the list print(list) # Iterate over the list for item in list: print("\t", item)[1, 2, 3, 4, 5] [1, 2, 4, 5] False True 4 4 [1, 2, 4, 5] 1 2 4 5This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
© Copyright Max Base, 2022