# Python program to test if all y occur after x in List

PythonServer Side ProgrammingProgramming

When it is required to check if all ‘y’ occurs after ‘x’ in a list, the enumerate attribute along with a specific condition is used.

## Example

Below is a demonstration of the same

my_list = [11, 25, 13, 11, 64, 25, 8, 9]

print("The list is :")
print(my_list)

x, y = 13, 8

x_index = my_list.index(x)

my_result = True
for index, element in enumerate(my_list):

if element == y and index < x_index:
my_result = False
break

if(my_result == True):
print("All y elements occcur after x elements")
else:
print("All y elements don't occcur after x elements")

## Output

The list is :
[11, 25, 13, 11, 64, 25, 8, 9]
All y elements occcur after x elements

## Explanation

• A list is defined and is displayed on the console.

• Two integer variables are initialized.

• The index values of the elements of the list are stored in a variable.

• A variable is set to Boolean ‘True’.

• The elements and the indices of the list are iterated using enumerate.

• Inside this, if the element being iterated and the second integer are equivalent and the index being iterated is less than index of the second integer, then the temporary variable is set to Boolean ‘False’.

• The control breaks out of the loop.

• In the end, based on the value of the temporary variable, the relevant message is displayed on the console.

Updated on 14-Sep-2021 10:55:09