To avoid "IndexError: list index out of range", you can use this piece of code.
list_values = [12, 112, 443] def getLastElement(lst): if len(lst) == 0: return "List is empty"0 else: return lst[-1] print(getLastElement(list_values)) Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalTo avoid "IndexError: list index out of range", you can use this piece of code.
list_values = [12, 112, 443] def getLastElement(lst): if len(lst) == 0: return "List is empty"0 else: return lst[-1] print(getLastElement(list_values)) To avoid "IndexError: list index out of range", you can use this piece of code.
list_values = [12, 112, 443] def getLastElement(lst): if len(lst) == 0: return "List is empty" else: return lst[-1] print(getLastElement(list_values)) To avoid "IndexError: list index out of range", you can use this piece of code.
list_values = [12, 112, 443] def getLastElement(lst): if len(lst) == 0: return 0 else: return lst[-1] print(getLastElement(list_values)) To avoid "IndexError: list index out of range", you can use this piece of code.
list_values = [12, 112, 443] def getLastElement(lst): if len(lst) == 0: return "List is empty" else: return lst[-1] print(getLastElement(list_values))