How to loop to check if all values in a list are bigger than the values in another list?

Viewed 868

As the question suggests, how do I do a loop to check if all values in a list are bigger than another set of values in another list? Assuming that the length of each list is the same and each value will be compared to another based on its index.

For example:

aList=[1,5,10,15,23]
bList=[0,4,9,14,22]
for x in range(len(aList)):
   if aList[x]>bList[x]:
      print("All values in aList are bigger than bList")
   else:
      print("Not all values in aList are bigger than bList")

This is what I have now, but due to the loop, I ended up printing 5 times. I only wish for the result to print once. Does anyone know a way around this?

8 Answers

Use all and zip

if all(a > b for a, b in zip(aList, bList)):
    print("All values in aList are bigger than bList")
else:
    print("Not all values in aList are bigger than bList")

Using numpy:

import numpy 

if np.all(np.array(aList) > np.array(bList)):
    print("All values in aList are bigger than bList")
else:
    print("Not all values in aList are bigger than bList")

Here is my implementation:

isGreater = True
for x in range(len(aList)):
    if aList[x]<bList[x]:
        isGreater = False
        break
if isGreater:
    print("List A is greater than List B")
else:
    print("List A is not greater than List B")

Taking the approach that you have done, simply set a flag variable, and after the loop check the flag and print the result. Below is the implementation

aList=[1,5,10,15,23]
bList=[0,4,9,14,22]
for x in range(len(aList)):
   if aList[x] < bList[x]:
      flag = True
      break
if flag:
    print('bList is bigger')
else:
    print('aList is bigger')

Try this:

aList=[1,5,10,15,23]
bList=[0,4,9,14,22]
aisbigger=True
for x in aList:
   for y in bList:
      if(x<y):
         aisbigger=False
if(aisbigger):
   print("All values in aList are bigger than bList")
else:
   print("Not all values in aList are bigger than bList")

after edit: if i am not wrong you want to compare every element only with same indexed element in the other list. then try this:

aList=[1,5,10,15,23]
bList=[0,4,9,14,22]
aisbigger=True
for x in range(len(aList)):
   if(aList[x]<bList[x]):
         aisbigger=False
if(aisbigger):
   print("All values in aList are bigger than bList")
else:
   print("Not all values in aList are bigger than bList")

You just need to make a minor tweak to your code. Rather than printing at each element just check the condition and at the end print depending on the value of the boolean variable, eachGreater in this case

aList=[1,5,10,15,23]
bList=[0,4,9,14,22]

eachGreater = True

for x in range(len(aList)):
    if aList[x]>bList[x]:
        pass
    else:
        eachGreater = False
        break

if eachGreater:
    print("All values in aList are bigger than bList")
else:
    print("Not all values in aList are bigger than bList")

Numpy approach using the module greater_equal

a=[1,5,10,15,23]
b=[0,4,9,14,22]

#Approach 1

if np.greater_equal(a,b).all():
    print("All values in aList are bigger than bList")
else:
    print("Not all values in aList are bigger than bList")

will give you

All values in aList are bigger than bList

and

re-run the code for a=[1,5,10,15,20] will give you

Not all values in aList are bigger than bList

Here’s the most straight forward solution. It’s a small adaptation from what you already have.

def each_item_larger(a, b):
    for pos in range(len(a)):
        if (a[pos] < b[pos]):
            return False
    return True

aList = [1, 5, 10, 15, 23]
bList = [0, 4, 9, 14, 22]

if each_item_larger(aList, bList):
    print(“Each item is larger.”)
else:
    print(“At least one item is smaller.”)
Related