Python - Remove characters till K element
Last Updated :
08 May, 2023
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', 'geeks']
# 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', 'geeks']
List elements after removing all before K : ['for', 'geeks']
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', 'geeks']
# 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', 'geeks']
List elements after removing all before K : ['for', 'geeks']
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', 'geeks']
#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
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks']
List elements after removing all before K : ['for', 'geeks']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using negative indexing and slicing
Approach:
- Finding the index of K
- Used negative indexing for slicing to access the elements till K
- Display the sliced list
Python3
#Python3 code to demonstrate
#Remove characters till K element
#using slicing
#Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
#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))
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks']
List elements after removing all before K : ['for', 'geeks']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using itertools.dropwhile():
Algorithm:
- Import the dropwhile() function from the itertools module.
- Initialize the test_list variable with a list of strings.
- Initialize the K variable with the string 'best'.
- Use the dropwhile() function to drop all elements from the test_list list until the K element is encountered.
- Create a new list res using list comprehension to filter out the K element from the result of dropwhile().
- Output the res list.
Python3
from itertools import dropwhile
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
#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.
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks']
List elements after removing all before K : ['for', 'geeks']
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', 'geeks']
# 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))
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks']
List elements after removing all before K: ['for', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python - Remove given character from first element of Tuple Given a Tuple list, remove K character from 1st element of the Tuple being String. Input : test_list = [("GF$g!", 5), ("!i$s", 4), ("best!$", 10)], K = '$' Output : [('GFg!', 5), ('!is', 4), ('best!', 10)] Explanation : First element's strings K value removed. Input : test_list = [("GF$g!", 5), ("be
5 min read
Python | Remove last character in list of strings Sometimes, we come across an issue in which we require to delete the last character from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus
8 min read
Python | Remove given character from Strings list Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Python - Remove N characters after K Given a String, remove N characters after K character. Input : test_str = 'ge@987eksfor@123geeks is best@212 for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : All 3 required occurrences removed. Input : test_str = 'geeksfor@123geeks is best for cs', N = 3, K = '@' Output
2 min read
Python - Remove Rear K characters from String List Sometimes, we come across an issue in which we require to delete the last characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plu
5 min read
Python | Remove Kth character from strings list Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be perfor
7 min read