Skip to content

Instantly share code, notes, and snippets.

@raeq
Created August 12, 2020 18:47
Show Gist options
  • Save raeq/32beae8906575da167102c0a4b8f4e10 to your computer and use it in GitHub Desktop.
Save raeq/32beae8906575da167102c0a4b8f4e10 to your computer and use it in GitHub Desktop.
A simple auto append list.
from collections import UserList
class AutoAppendList(UserList):
"""
AutoAppendList. Will append an item if you are off by one index assignment.
>>> aal: AutoAppendList = AutoAppendList()
>>> for i in range(10):
... aal[i] = i
>>> print(aal)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
def __setitem__(self, index, item):
"""__setitem__.
Args:
index:
item:
"""
if index == len(self.data):
self.data.append(item)
else:
self.data[i] = item
aal: AutoAppendList = AutoAppendList()
for iteration, char in enumerate("hello"):
aal[iteration] = ord(char)
assert aal == [104, 101, 108, 108, 111]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment