我想知道是否有一种更 Pythonic的方式可以将元素添加到列表中,具体取决于(默认)索引。如果索引超出范围会发生什么。(我来自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
提前致谢。