Open In App

Python program to Test if all y occur after x in List

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, test if all occurrences of y are after the occurrence of x in the list.

Input : test_list = [4, 5, 6, 2, 4, 5, 2, 9], x, y = 6, 2 
Output : True 
Explanation : All occurrences of 2 are after 6, hence true.

Input : test_list = [4, 2, 5, 6, 2, 4, 5, 2, 9], x, y = 6, 2 
Output : False 
Explanation : All occurrences of 2 are not after 6, hence true. 

Method #1 : Using loop + index()

In this, we check for an index of x in the list, and then run a loop to get the occurrence of y, if any y occurs before x index, the result is False.

Python3




# Python3 code to demonstrate working of
# Test if y occurs after x in List
# Using loop + index()
 
# initializing list
test_list = [4, 5, 6, 2, 4, 5, 2, 9]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing x, y
x, y = 6, 2
 
# getting index of x
xidx = test_list.index(x)
 
res = True
for idx, ele in enumerate(test_list):
     
    # checking for y and comparing index
    if ele == y and idx < xidx:
        res = False
        break
 
# printing result
print("Do all y occur after x : " + str(res))


 
Output:

The original list is : [4, 5, 6, 2, 4, 5, 2, 9]
Do all y occur after x : True

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 : Using all() + index()

In this, we test for all the indices of y using all(), and index() is used to get the index of x in the list.

Python3




# Python3 code to demonstrate working of
# Test if y occurs after x in List
# Using all() + index()
 
# initializing list
test_list = [4, 5, 6, 2, 4, 5, 2, 9]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing x, y
x, y = 6, 2
 
# getting index of x
xidx = test_list.index(x)
 
# checking for all indices of y in list
res = all(idx > xidx for idx, ele in enumerate(test_list) if ele == y)
 
# printing result
print("Do all y occur after x : " + str(res))


 
Output:

The original list is : [4, 5, 6, 2, 4, 5, 2, 9]
Do all y occur after x : True

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), no extra space required

Method #3 : Using count(),index() methods

Approach:

  1. Store the count of y in test_list in a(using count())
  2. Find the index of x in test_list and store in b
  3. Slice the test_list from b to end and find the count of y in it and store in z(using count())
  4. Check if a==z, if yes which means y occurs after all x in list then set res to True, otherwise set res to False
  5. Display res

Python3




# Python3 code to demonstrate working of
# Test if y occurs after x in List
 
# initializing list
test_list = [4, 5, 6, 2, 4, 5, 2, 9]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing x, y
x, y = 6, 2
 
# Testing whether y is occuring after x in our list
a = test_list.count(y)
b = test_list.index(x)
z = test_list[b:].count(y)
res = a == z
 
# printing result
print("Do all y occur after x : " + str(res))


Output

The original list is : [4, 5, 6, 2, 4, 5, 2, 9]
Do all y occur after x : True

Time Complexity : O(N) N – length of test_list.
Auxiliary Space: O(1) because only a single variable is used to store the result.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads