Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Remove characters till K element

Python – Remove characters till K element

Sometimes, while working with Python, we can have problem in which we need to get all elements in list occurring after particular character in list. This kind of problem can have application in data domains and web development. Lets discuss certain ways in which this task can be performed. 

Method #1: Using loop 

This is brute force method in which this task can be performed. In this, we iterate the loop till we find K and then start appending characters, seeming like removing the elements before K. 

Python3




# Python3 code to demonstrate
# Remove characters till K element
# using loop
 
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 'best'
 
# Remove characters till K element
# using loop
res = []
flag = 0
for ele in test_list:
    if ele == K:
        flag = 1
        continue
    if flag:
        res.append(ele)
 
# printing result
print("List elements after removing all before K : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best', 'for', 'neveropen']
List elements after removing all before K : ['for', 'neveropen']

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #2: Using index() + list comprehension

This is yet another way in which this task can be performed. In this, we first find the index of element and then use list comprehension + enumerate() to append only elements after that K. 

Python3




# Python3 code to demonstrate
# Remove characters till K element
# using list comprehension + enumerate() + index()
 
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 'best'
 
# Remove characters till K element
# using list comprehension + enumerate() + index()
temp = test_list.index(K)
res = [ele for idx, ele in enumerate(test_list) if idx > temp]
 
# printing result
print("List elements after removing all before K : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best', 'for', 'neveropen']
List elements after removing all before K : ['for', 'neveropen']

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3 : Using slice

This is the most pythonic way to perform this task. In this, we use slicing to get the elements from the index of K till end.

Python3




#Python3 code to demonstrate
#Remove characters till K element
#using slicing
#Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
#printing original list
print("The original list is : " + str(test_list))
 
#Initializing K
K = 'best'
 
#Remove characters till K element
#using slicing
temp = test_list.index(K)
res = test_list[temp + 1:]
 
#printing result
print ("List elements after removing all before K : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : ['gfg', 'is', 'best', 'for', 'neveropen']
List elements after removing all before K : ['for', 'neveropen']

Time complexity: O(n) 
Auxiliary Space: O(n) 

Method #4: Using negative indexing and slicing

Approach:

  1. Finding the index of K
  2. Used negative indexing for slicing to access the elements till K
  3. Display the sliced list

Python3




#Python3 code to demonstrate
#Remove characters till K element
#using slicing
#Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
#printing original list
print("The original list is : " + str(test_list))
 
#Initializing K
K = 'best'
 
#Remove characters till K element
#using slicing
temp = test_list.index(K)
res = test_list[-temp:]
 
#printing result
print ("List elements after removing all before K : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'neveropen']
List elements after removing all before K : ['for', 'neveropen']

Time complexity: O(n) 
Auxiliary Space: O(n) 

Method #5 : Using itertools.dropwhile():

Algorithm:

  1. Import the dropwhile() function from the itertools module.
  2. Initialize the test_list variable with a list of strings.
  3. Initialize the K variable with the string ‘best’.
  4. Use the dropwhile() function to drop all elements from the test_list list until the K element is encountered.
  5. Create a new list res using list comprehension to filter out the K element from the result of dropwhile().
  6. Output the res list.

Python3




from itertools import dropwhile
 
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
#printing original list
print("The original list is : " + str(test_list))
  
K = 'best'
 
res = [x for x in dropwhile(lambda x: x != K, test_list) if x != K]
#printing result
print ("List elements after removing all before K : " + str(res))
#This code  is contributed by Jyothi pinjala.


Output

The original list is : ['gfg', 'is', 'best', 'for', 'neveropen']
List elements after removing all before K : ['for', 'neveropen']

Time complexity : O(N) where N is the length of the list. The time complexity of list comprehension is also O(N) because it iterates over each element of the list. Therefore, the overall time complexity of the code is O(N), where N is the length of the list.
Auxiliary space :O(N), where N is the length of the list. This is because we are creating a new list res with the same number of elements as the input list, and therefore the space required is proportional to the length of the list.

Method 6: using the built-in map() function along with lambda function

Python3




# Python3 code to demonstrate
# Remove characters till K element
# using map() + lambda
 
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 'best'
 
# Remove characters till K element
# using map() + lambda
res = list(map(lambda ele: ele, test_list[test_list.index(K) + 1:]))
 
# printing result
print("List elements after removing all before K: " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'neveropen']
List elements after removing all before K: ['for', 'neveropen']

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