Python | Count of Matching i, j index elements
Last Updated :
09 Apr, 2023
Sometimes, while programming, we can have a problem in which we need to check for ith and jth character of each string. We may require to extract count of all strings with similar ith and jth characters. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute force method by which this task can be performed. In this, iterate each element of list and check for each string’s ith and jth character and increase the counter in case we find a match.
Python3
# Python3 code to demonstrate working of
# Count of Matching i, j index elements
# Using loop
# initialize list
test_list = ['geeks', 'beke', 'treat', 'neke']
# printing original list
print("The original list : " + str(test_list))
# initialize i
i = 1
# initialize j
j = 3
# Count of Matching i, j index elements
# Using loop
count = 0
for ele in test_list:
if ele[i] == ele[j]:
count = count + 1
# printing result
print("Total Strings with similar ith and jth elements : " + str(count))
Output : The original list : ['geeks', 'beke', 'treat', 'neke']
Total Strings with similar ith and jth elements : 2
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using sum() + generator expression This is one liner alternative to perform this task. In this, we perform the task of iteration using generator expression and summation using sum().
Python3
# Python3 code to demonstrate working of
# Count of Matching i, j index elements
# Using sum() + generator expression
# initialize list
test_list = ['geeks', 'beke', 'treat', 'neke']
# printing original list
print("The original list : " + str(test_list))
# initialize i
i = 1
# initialize j
j = 3
# Count of Matching i, j index elements
# Using sum() + generator expression
res = sum(1 for ele in test_list if ele[i] == ele[j])
# printing result
print("Total Strings with similar ith and jth elements : " + str(res))
Output : The original list : ['geeks', 'beke', 'treat', 'neke']
Total Strings with similar ith and jth elements : 2
Time Complexity: O(n) where n is the number of elements in the string list. The sum() + generator expression is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) additional constant space is required
Method #3 : Another approach can be using filter() function and lambda function.
Python3
# Python3 code to demonstrate working of
# Count of Matching i, j index elements
# Using filter() + lambda
# initialize list
test_list = ['geeks', 'beke', 'treat', 'neke']
# printing original list
print("The original list : " + str(test_list))
# initialize i
i = 1
# initialize j
j = 3
# Count of Matching i, j index elements
# Using filter() + lambda
result = len(list(filter(lambda x: x[i] == x[j], test_list)))
# printing result
print("Total Strings with similar ith and jth elements : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : ['geeks', 'beke', 'treat', 'neke']
Total Strings with similar ith and jth elements : 2
In this approach, we are using the filter() function along with a lambda function to filter out elements from the list test_list that match the condition of having the same value at index i and index j. We then use the len() function to count the number of elements in the filtered list, which gives us the total count of strings with similar ith and jth elements. The time complexity of this approach is O(n) as we are iterating through the list once and the space complexity is O(n) as we are creating a new filtered list.
Similar Reads
Python | Matching elements count Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the count of indices of the match. Querying whole list for the this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause.
5 min read
Python - Matrix elements Frequencies Counter Sometimes, while working with python Matrix, we can have a problem in which we need to find frequencies of all elements in Matrix. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using Counter() + sum() + map() The
5 min read
Python - Count elements in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read
Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform
2 min read
Python - Elements frequency count in multiple lists Sometimes while working with Python lists we can have a problem in which we need to extract the frequency of elements in list. But this can be added work if we have more than 1 list we work on. Let's discuss certain ways in which this task can be performed. Method #1: Using dictionary comprehension
6 min read
Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a
2 min read