Python | Consecutive String Comparison
Last Updated :
09 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to perform comparison between a string and it's next element in a list and return all strings whose next element is similar list. Let's discuss certain ways in which this task can be performed.
Method #1 : Using zip() + loop This is one way in which this task can be performed. In this, we use zip() to combine the element and it's next element and then compare for truth and save it in list.
Python3
# Python3 code to demonstrate working of
# Consecutive String Comparison
# using zip() + loop
# initialize list
test_list = ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks']
# printing original list
print("The original list : " + str(test_list))
# Consecutive String Comparison
# using zip() + loop
res = []
for i, j in zip(test_list, test_list[1: ]):
if i == j:
res.append(i)
# printing result
print("List of Consecutive similar elements : " + str(res))
Output : The original list : ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks']
List of Consecutive similar elements : ['gfg', 'best', 'geeks']
Time Complexity: O(n*nlogn) where n is the number of elements in the string list. The zip() + loop is used to perform the task and it takes O(n*nlogn) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.
Method #2 : Using list comprehension + zip() This task can also be performed using above functionalities. In this, we use one-liner approach to solve this problem using list comprehension. The method is similar to above one.
Python3
# Python3 code to demonstrate working of
# Consecutive String Comparison
# using zip() + list comprehension
# initialize list
test_list = ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks']
# printing original list
print("The original list : " + str(test_list))
# Consecutive String Comparison
# using zip() + list comprehension
res = [i for (i, j) in zip(test_list, test_list[1:]) if i == j]
# printing result
print("List of Consecutive similar elements : " + str(res))
Output : The original list : ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks']
List of Consecutive similar elements : ['gfg', 'best', 'geeks']
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list comprehension + zip() which has a time complexity of O(n*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 #3 : Using enumerate() + loop
This problem can also be solved by using enumerate() function to access the elements and it’s index and then performing comparison for consecutive elements.
Python3
# Python3 code to demonstrate working of
# Consecutive String Comparison
# using enumerate() + loop
# initialize list
test_list = ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks']
# printing original list
print("The original list : " + str(test_list))
# Consecutive String Comparison
# using enumerate() + loop
res = []
for i, j in enumerate(test_list):
if i + 1 != len(test_list):
if j == test_list[i + 1]:
res.append(j)
# printing result
print("List of Consecutive similar elements : " + str(res))
#this code is contributed by edula vinay kumar reddy
OutputThe original list : ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks']
List of Consecutive similar elements : ['gfg', 'best', 'geeks']
Time complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Case-insensitive string comparison in Python The goal here is to compare strings in a list without considering their case, meaning the comparison should ignore whether letters are uppercase or lowercase. For example, given the list ["hello", "HELLO", "HeLLo"], we want to check if all strings are equal, ignoring the case. Let's explore differen
2 min read
Python - Equidistant consecutive characters Strings Given a Strings List, extract all the strings, whose consecutive characters are at the common difference in ASCII order. Input : test_list = ["abcd", "egil", "mpsv", "abd"] Output : ['abcd', 'mpsv'] Explanation : In mpsv, consecutive characters are at distance 3. Input : test_list = ["abcd", "egil",
9 min read
Python - First K consecutive digits in String Given a String and number K, extract first K consecutive digits making number. Input : test_str = "geeks5geeks43best", K = 2 Output : 43 Explanation : 43 is first 2 consecutive digits. Input : test_str = "geeks5gee2ks439best", K = 3 Output : 439 Explanation : 439 is first 3 consecutive digits. Metho
5 min read
Check for ASCII String - Python To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria.Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method,
2 min read
Python - Similar characters Strings comparison Given two Strings, separated by delim, check if both contain same characters. Input : test_str1 = 'e!e!k!s!g', test_str2 = 'g!e!e!k!s', delim = '!' Output : True Explanation : Same characters, just diff. positions. Input : test_str1 = 'e!e!k!s', test_str2 = 'g!e!e!k!s', delim = '!' Output : False Ex
6 min read
Python - Maximum Consecutive Substring Occurrence Sometimes, while working with Python, we can have a problem in which we must check for substrings occurring in consecutive repetition. This can have applications in data domains. Let us discuss a way in which this task can be performed. Method #1: Using max() + re.findall() A combination of the abov
5 min read
Find Longest Consecutive Letter and Digit Substring - Python The task is to identify the longest sequence of consecutive letters or digits in a given string. A consecutive letter or digit refers to a substring where each character is adjacent to the next one in the alphabet for letters or numerically for digits. It involves identifying the longest sequence of
4 min read
Check if string contains character - Python We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True.Using in Operatorin operator is the easiest way to check if a c
2 min read
Find Length of String in Python In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop.
2 min read
Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si
2 min read