I'd like to check whether the current element in my list is larger than all the other previous elements in the list, within a for loop in Python:
list_height = [187, 241, 319, 213, 541, 259, 319, 381] # an example, input depends on the user
length = len(list_height)
list_floor2 = [] # empty list for storage purposes
# For loop
for i in range(length):
floor1 = list_height[i]
if i == 0:
list_floor2.append(floor2)
elif list_height[i] > list_height[0] and list_height[i] > list_height[i - 1]:
floor1 = list_hoogte[i - 1]
list_floor2.append(floor2)
Instead of list_height[i] > list_height[i - 1] in the elif statement I'd like to not only check whether the current index (i) > the previous element in list_height (so not only index i - 1), BUT whether current index (i) > all previous elements in list_height.
How can I select all previous elements in the list? I've tried all() and list_height[:i] but I always get the same error:
TypeError: '>' not supported between instances of 'int' and 'list'
NameError: name 'floor2' is not defined. Did you mean: 'floor1'?)! Also, can you provide your expected output, so people can ensure their code works to your expectations, please?a[i]is larger thana[i-1], a[i-2]etc. Now, when you checka[i+1], do you really need to check all of the previous elementsa[i], a[i-1], a[i-2]? Or just one of them? If yes, which one?