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
fromArrayListimportArrayList# Create a new listlist=ArrayList(10) # Add items to the listlist.add(1) list.add(2) list.add(3) list.add(4) list.add(5) # Print the listprint(list) # Remove an item from the listlist.remove(3) # Print the listprint(list) # Check if an item is in the listprint(list.search(3)) print(list.search(4)) # Get an item from the listprint(list.get(2)) # Print the length of the listprint(len(list)) # Print the listprint(list) # Iterate over the listforiteminlist: print("\t", item)
[1, 2, 3, 4, 5] [1, 2, 4, 5] False True 4 4 [1, 2, 4, 5] 1 2 4 5
This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
© Copyright Max Base, 2022