I would like to know if there is a more pythonic way to add an element to a list, depending on an (default) index. And what happens if the index is out of bounds. (I'm coming from Java)
self.releases = []
def add_release(self, release, index=-1):
list_length = len(self.releases)
if index < 0:
# add element at the end of the list
self.releases.append(release)
elif index < list_length:
# add element at the given index
self.releases.insert(index, release)
else:
# index is out of bound ~ what will happen when an element will be added at this index?
pass
Thanks in advance.
appendaccepts1argument and adds it at the end. Maybe you wantìnsert.