Python – Consecutive element deletion strings

Sometimes, while working with Python, we can have a problem in which we have a string and wish to extract all possible combination of words after deletion of consecutive elements one at a time. This can have application in many domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension + list slicing
This is one of the way in which this task can be performed. In this, we iterate for the elements of list and keep on creating new string using consecutive list slicing.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive element deletion strings
# Using list comprehension + list slicing
  
# initializing string
test_str = 'Geeks4Geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Consecutive element deletion strings
# Using list comprehension + list slicing
res =  [test_str[: idx] + test_str[idx + 1:] 
             for idx in range(len(test_str))]
  
# printing result 
print("Consecutive Elements removal list : " + str(res)) 

chevron_right


Output :

The original string is : Geeks4Geeks
Consecutive Elements removal list : [‘eeks4Geeks’, ‘Geks4Geeks’, ‘Geks4Geeks’, ‘Gees4Geeks’, ‘Geek4Geeks’, ‘GeeksGeeks’, ‘Geeks4eeks’, ‘Geeks4Geks’, ‘Geeks4Geks’, ‘Geeks4Gees’, ‘Geeks4Geek’]

 



Method #2 : Using list comprehension + enumerate()
The combination of above methods can be used to perform this task. In this, we extract the index using enumerate. This gives more cleaner code.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive element deletion strings
# Using list comprehension + enumerate()
  
# initializing string
test_str = 'Geeks4Geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Consecutive element deletion strings
# Using list comprehension + enumerate()
res = [test_str[:idx] + test_str[idx + 1:] 
          for idx, _ in enumerate(test_str)]
  
# printing result 
print("Consecutive Elements removal list : " + str(res)) 

chevron_right


Output :

The original string is : Geeks4Geeks
Consecutive Elements removal list : [‘eeks4Geeks’, ‘Geks4Geeks’, ‘Geks4Geeks’, ‘Gees4Geeks’, ‘Geek4Geeks’, ‘GeeksGeeks’, ‘Geeks4eeks’, ‘Geeks4Geks’, ‘Geeks4Geks’, ‘Geeks4Gees’, ‘Geeks4Geek’]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.