Home > Software design >  Check all previous elements from list in for loop Python
Check all previous elements from list in for loop Python

Time:10-10

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'

CodePudding user response:

You can do this with enumerate and max,

[v for i, v in enumerate(list_height) if v >= max(list_height[:i 1])]

# Output
[187, 241, 319, 541]

CodePudding user response:

As already in the comments mentioned, you don't actually need to check every single element till the current index. Check in each iteration for the maximum value, if the current number is bigger, you have a current maximum currrent_max, else you continue. If there is a new maximum (which automatically is also bigger than every number before), you assign that number as current_max and append the current value in the second list.

list_height = [187, 241, 319, 213, 541, 259, 319, 381]
list_floor2 = []

for i, num in enumerate(list_height):
    if i==0:
        list_floor2.append(num)
        current_max = num
    else:
        if num >= current_max:
            current_max = num
            list_floor2.append(num)

print(list_floor2)
[187, 241, 319, 541]

CodePudding user response:

Here is my solution:

The for-loop will iterate through the list_height list. And using the all function, will check to see if the current value (value[1]) is greater than any previous number in the list. If True, it will print the line with a True value at the end, otherwise it will print a False value at the end.

list_height = [187, 241, 319, 213, 541, 259, 319, 381]

for value in enumerate(list_height):
    if all(x < value[1] for x in list_height[0:value[0]]):
        print(f"{value[0]}-{value[1]} : True")
    else:
        print(f"{value[0]}-{value[1]} : False")

OUTPUT:

0-187 : True
1-241 : True
2-319 : True
3-213 : False
4-541 : True
5-259 : False
6-319 : False
7-381 : False

Or if you want more succinct code:

list_height = [187, 241, 319, 213, 541, 259, 319, 381]

for i,num in enumerate(list_height):
    val = all(x < num for x in list_height[0:i])
    print(f"{i}-{num} : {val}")

This provides the same output.

CodePudding user response:

length = len(list_height)
list_floor2 = [] # empty list for storage purposes

# check whether the current element in my list is larger than all the other previous elements in the list
def Check(i):
    is_larger=True
    for j in range(i):
        if(list_height[j] > list_height[i]):
            is_larger=False
            break
    return is_larger 
# For loop
for i in range(length):
    floor1 = list_height[i]
    if Check(i):
        list_floor2.append(floor1)
  • Related