Python program to Occurrences of i before first j in list
Last Updated :
25 Apr, 2023
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
test_list = [ 4 , 5 , 6 , 4 , 1 , 4 , 8 , 5 , 4 , 3 , 4 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 4 , 8
res = 0
for ele in test_list:
if ele = = j:
break
if ele = = i:
res + = 1
print ( "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
test_list = [ 4 , 5 , 6 , 4 , 1 , 4 , 8 , 5 , 4 , 3 , 4 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 4 , 8
jidx = test_list.index(j)
temp = test_list[:jidx]
res = temp.count(i)
print ( "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
test_list = [ 4 , 5 , 6 , 4 , 1 , 4 , 8 , 5 , 4 , 3 , 4 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 4 , 8
res = 0
x = test_list.index(j)
y = test_list[:x]
import operator
res = operator.countOf(y,i)
print ( "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 #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
test_list = [ 4 , 5 , 6 , 4 , 1 , 4 , 8 , 5 , 4 , 3 , 4 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 4 , 8
j_index = test_list.index(j)
count_i = sum ([ 1 for ele in test_list[:j_index] if ele = = i])
print ( "Number of i's till 1st j : " + str (count_i))
|
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), 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
test_list = [ 4 , 5 , 6 , 4 , 1 , 4 , 8 , 5 , 4 , 3 , 4 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 4 , 8
count_i = 0
for ele in test_list:
if ele = = j:
break
if ele = = i:
count_i + = 1
print ( "Number of i's till 1st j : " + str (count_i))
|
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), 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
import itertools
test_list = [ 4 , 5 , 6 , 4 , 1 , 4 , 8 , 5 , 4 , 3 , 4 , 9 ]
print ( "The original list is : " + str (test_list))
i, j = 4 , 8
count_i = sum ( 1 for x in itertools.takewhile(
lambda x: x ! = j, test_list) if x = = i)
print ( "Number of i's till 1st j : " + str (count_i))
|
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), where n is the length of the input list.
Auxiliary space: O(1), as it does not create any additional data structures.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...