Sunday, December 29, 2024
Google search engine
HomeLanguagesPython | Consecutive String Comparison

Python | Consecutive String Comparison

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', 'Lazyroar', 'Lazyroar']
 
# 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', 'Lazyroar', 'Lazyroar']
List of Consecutive similar elements : ['gfg', 'best', 'Lazyroar']

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', 'Lazyroar', 'Lazyroar']
 
# 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', 'Lazyroar', 'Lazyroar']
List of Consecutive similar elements : ['gfg', 'best', 'Lazyroar']

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', 'Lazyroar', 'Lazyroar']
   
# 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


Output

The original list : ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'Lazyroar', 'Lazyroar']
List of Consecutive similar elements : ['gfg', 'best', 'Lazyroar']

Time complexity: O(n)

Auxiliary Space: O(n)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments