Apr-18-2018, 04:04 PM
Actually the list slicing is not trowing any exception, so you can use it also in situation when you have less items in a list (at least in Python 3):
l1 = ['hello'] for word in l1[0:3]: print(word)If you want to iterate through all items that you have in a list at the moment, you can make a slice of the current length like this:
l1 = ['hello', 'bye', 'now', 'before', 'after'] for word in l1[0:len(l1)]: print(word)So the out of range can never happen.