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 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 = 0for 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)) |
The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] List elements after removing all before K : ['for', 'geeks']
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 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)) |
The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] List elements after removing all before K : ['for', 'geeks']
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.
Recommended Posts:
- Python | Getting sublist element till N
- Python | Get elements till particular element in list
- Python - Maximum element till K value
- Python | Get Kth element till N
- Python | Values till False element
- Python | Combinations of elements till size N in list
- Python | Prefix Sum Subarray till False value
- Python | Repeat String till K
- Python | Count the elements till first tuple
- Python | Print Alphabets till N
- Python - String till Substring
- Python | Multiplication till Null value
- Python | Values Frequency till Maximum K
- Python - Elements Product till K value
- Python - All Possible unique K size combinations till N
- Python - Slice till K dictionary value lists
- Python - Elements Maximum till current index in List
- Python - Extract String till Numeric
- Python - Group concatenate Till K
- Python program to Extract string till first Non-Alphanumeric character
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.

