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.
Similar Reads
Python - First Occurrence of One List in Another
We are given two lists, and our task is to find the first occurrence of the second list as a contiguous subsequence in the first list. If the second list appears as a subsequence inside the first list, we return the starting index of its first occurrence; otherwise, we return -1. For example, if a =
4 min read
Last Occurrence of Some Element in a List - Python
We are given a list we need to find last occurrence of some elements in list. For example, w = ["apple", "banana", "orange", "apple", "grape"] we need to find last occurrence of string 'apple' so output will be 3 in this case. Using rindex()rindex() method in Python returns the index of the last occ
3 min read
Python program to Test if all y occur after x in List
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 Explan
4 min read
Python - Occurrence counter in List of Records
Sometimes, while dealing with records we can have a problem in which we need count the occurrence of incoming digits corresponding to different characters/players in a game and compile them in a dictionary. This can have application in gaming and web development. Lets discuss a way in which this can
2 min read
Python - Get the indices of all occurrences of an element in a list
We are given a list and our task is to find all the indices where a particular element occurs. For example, if we have a list like [1, 2, 3, 2, 4, 2] and the element is 2, then the output will be [1, 3, 5] because 2 appears at these positions. Using List ComprehensionList comprehension allows for a
2 min read
Python - First Occurrence of True number
We are given a list of True and False we need to find first occurrence of True. For example we are having a list a = [False, False, True, False, True] so we need to return first occurrence of True so that output should be 2 in this case. Using index()index() method in Python returns index of first o
2 min read
Python program to find the occurrence of substring in the string
Given a list of words, extract all the indices where those words occur in the string. Input : test_str = 'geeksforgeeks is best for geeks and cs', test_list = ["best", "geeks"] Output : [2, 4] Explanation : best and geeks occur at 2nd and 4th index respectively. Input : test_str = 'geeksforgeeks is
4 min read
Python - Substitute K for first occurrence of elements
Sometimes, while working with Python data, we can have a problem in which, we need to perform a substitution to the first occurrence of each element in list. This type of problem can have application in various domains such as web development. Let's discuss certain ways in which this task can be per
6 min read
Python Program to calculate Dictionaries frequencies
Given a list of dictionaries, the task here is to write a python program to extract dictionary frequencies of each dictionary. Input : test_list = [{'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 6, 'is' : 3, 'best' : 8}, {'gfg' : 1, 'is' : 4, 'best' : 9}, {'gfg' : 1, 'is' : 1, 'best' : 9}, {'gfg' : 6,
6 min read
Python program to find String in a List
Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e
3 min read