Python program to Occurrences of i before first j in list

Given a list, the task is to write a Python program to count the occurrences of ith element before the first occurrence of jth element.
Examples:
Input : test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9], i, j = 4, 8
Output : 3
Explanation : 4 occurs 3 times before 8 occurs.Input : test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9], i, j = 5, 8
Output : 1
Explanation : 5 occurs 1 time before 8 occurs.
Method #1: Using loop
In this, we increment counter whenever i is encountered and stop when any j has occurred, i.e breaking from the loop.
Python3
# Python3 code to demonstrate working of# Occurrences of i before first j# Using loop# initializing Matrixtest_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]# printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 4, 8res = 0for ele in test_list: # breaking on 1st j if ele == j: break # counting i till 1st j if ele == i: res += 1# printing resultprint("Number of i's till 1st j : " + str(res)) |
Output:
The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] Number of i's till 1st j : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using index() + slicing + count()
In this, we perform the task of getting the index of j, and then slice list till there, count() is used to get a count of i in the sliced list to get the required result.
Python3
# Python3 code to demonstrate working of# Occurrences of i before first j# Using index() + slicing + count()# initializing Matrixtest_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]# printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 4, 8# getting indexjidx = test_list.index(j)# slicing listtemp = test_list[:jidx]# getting countres = temp.count(i)# printing resultprint("Number of i's till 1st j : " + str(res)) |
Output:
The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] Number of i's till 1st j : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using index(), slicing, operator.countOf() methods
- Find the index of j using index()
- Slice the list till the index.
- Find the occurrence of i in that sliced list(using operator.countOf())
- Display the occurrence.
Example:
Python3
# Python3 code to demonstrate working of# Occurrences of i before first j# Using loop# initializing Matrixtest_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] # printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 4, 8res = 0x=test_list.index(j)y=test_list[:x]import operatorres=operator.countOf(y,i)# printing resultprint("Number of i's till 1st j : " + str(res)) |
The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] Number of i's till 1st j : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using index() and list comprehension
This method finds the index of the first occurrence of j in the list using the index() method, and then uses list comprehension and slicing to count the occurrences of i before the first occurrence of j.
Python3
# Python3 code to demonstrate working of# Occurrences of i before first j# Using index() and list comprehension# initializing Matrixtest_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]# printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 4, 8# finding index of first j in listj_index = test_list.index(j)# using list comprehension to count i's before first jcount_i = sum([1 for ele in test_list[:j_index] if ele == i])# printing resultprint("Number of i's till 1st j : " + str(count_i)) |
The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] Number of i's till 1st j : 3
Time complexity: O(n), where n is the length of the list,
Auxiliary space: O(1), as it only uses a few variables to store the index and the count.
Method 5: Using for loop
- Initialize the input list test_list.
- Print the original list using the print() function and the string concatenation operator +.
- Initialize the variables i and j to represent the values we are looking for.
- Initialize the variable count_i to 0 to keep track of the number of occurrences of i before the first occurrence of j.
- Loop through the elements of the list using a for loop.
- For each element, check if it is equal to j. If it is, exit the loop.
- Otherwise, check if the element is equal to i. If it is, increment the count_i variable.
- After the loop has finished, print the value of count_i.
Example:
Python3
# Python3 code to demonstrate working of# Occurrences of i before first j# Using for loop# initializing Matrixtest_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]# printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 4, 8# initializing count_icount_i = 0# looping through the list until the first occurrence of j is foundfor ele in test_list: if ele == j: break if ele == i: count_i += 1# printing resultprint("Number of i's till 1st j : " + str(count_i)) |
The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] Number of i's till 1st j : 3
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1) as we only use a constant amount of extra space to store the count_i variable.
Method 6: using the itertools.takewhile() function
Python3
# Python3 code to demonstrate working of# Occurrences of i before first j# Using itertools.takewhile()import itertools# initializing Matrixtest_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9]# printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 4, 8# count the occurrences of i before the first occurrence of jcount_i = sum(1 for x in itertools.takewhile( lambda x: x != j, test_list) if x == i)# printing resultprint("Number of i's till 1st j : " + str(count_i)) |
The original list is : [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] Number of i's till 1st j : 3
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as it does not create any additional data structures.



