JT321 JT321 - 1 month ago 15
Python Question

Generating pairs from list in python in order to determine if either value in pair is above or below certain numerical value

Python: How to pair the first two numerical items in a list and determine whether item a or item b are greater a certain number, and create a loop so it does this for the entire list?

I have tried creating a nested list of the pairs, but I cannot convert these into floats in order for to determine whether either item a or b is greater than a certain numerical value.

Example
List = [ 4, 7, 3 , 6, 9, 6)

I need to split the list into pairs and then see if either numbers in the pair is greater or equal to 5 and then print yes or no.
Thanks

Answer

you can use the by-step function of the slicing tool:

Example_List = [ 4, 7, 3 , 6, 9, 6]
for a,b in zip(Example_List[::2],Example_List[1::2]):
    if max(a,b)>5: 
        print("yes")
    else:
        print("no")

Example_List[::2] takes one element out of two starting from the first one and Example_List[1::2] takes one element out of two starting from the second one