Sometimes, while working with Python list, we can have a requirement in which we need to remove all the elements after a particular element, or, get all elements before a particular element. These both are similar problems and having a solution to it is always helpful. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using index() + list slicing
This problem can be solved using the combination of these functions. The index() can be used to find the index of desired element and list slicing can perform the remaining task of getting the elements.
# Python3 code to demonstrate working of # Get elements till particular element in list # using index() + list slicing # initialize list test_list = [1, 4, 6, 8, 9, 10, 7] # printing original list print("The original list is : " + str(test_list)) # declaring elements till which elements required N = 8 # Get elements till particular element in list # using index() + list slicing temp = test_list.index(N) res = test_list[:temp] # printing result print("Elements till N in list are : " + str(res)) |
The original list is : [1, 4, 6, 8, 9, 10, 7] Elements till N in list are : [1, 4, 6]
Method #2 : Using generator
This task can also be performed using the generator function which uses yield to get the elements just till the required element and breaks the yields after that element.
# Python3 code to demonstrate working of # Get elements till particular element in list # using generator # helper function to perform task def print_ele(test_list, val): for ele in test_list: if ele == val: return yield ele # initialize list test_list = [1, 4, 6, 8, 9, 10, 7] # printing original list print("The original list is : " + str(test_list)) # declaring elements till which elements required N = 8 # Get elements till particular element in list # using generator res = list(print_ele(test_list, N)) # printing result print("Elements till N in list are : " + str(res)) |
The original list is : [1, 4, 6, 8, 9, 10, 7] Elements till N in list are : [1, 4, 6]
Recommended Posts:
- Python | Combinations of elements till size N in list
- Python - Elements Maximum till current index in List
- Python | Replace elements in second list with index of same element in first list
- Python | Subtract two list elements if element in first list is greater
- Python | Get Kth element till N
- Python | Getting sublist element till N
- Python - Maximum element till K value
- Python - Remove characters till K element
- Python | Values till False element
- Python | Count the elements in a list until an element is a Tuple
- Python - Elements Product till K value
- Python | Count the elements till first tuple
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Insert Nth element to Kth element in other list
- Python | Move one list element to another list
- Python | Merge List with common elements in a List of Lists
- Python - Assigning Key values to list elements from Value list Dictionary
- Python | Add list elements with a multi-list based on index
- Python | Sorting list of lists with similar list elements
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.

