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 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() + loopres = []for i, j in zip(test_list, test_list[1: ]): if i == j: res.append(i) # printing resultprint("List of Consecutive similar elements : " + str(res)) |
The original list : ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks'] List of Consecutive similar elements : ['gfg', 'best', 'geeks']
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 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 comprehensionres = [i for (i, j) in zip(test_list, test_list[1:]) if i == j] # printing resultprint("List of Consecutive similar elements : " + str(res)) |
The original list : ['gfg', 'gfg', 'is', 'best', 'best', 'for', 'geeks', 'geeks'] List of Consecutive similar elements : ['gfg', 'best', 'geeks']



