
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Extract element from a list succeeded by K
When it is required to extract element from a list succeeded by ‘K’, a simple iteration and the ‘append’ method is used.
Example
Below is a demonstration of the same
my_list = [45, 65, 32, 78, 99, 10, 21, 2] print("The list is : ") print(my_list) K = 99 print("The value of K is ") print(K) my_result = [] for elem in range(len(my_list) - 1): if my_list[elem + 1] == K: my_result.append(my_list[elem]) print("The result is : " ) print(my_result)
Output
The list is : [45, 65, 32, 78, 99, 10, 21, 2] The value of K is 99 The result is : [78]
Explanation
A list is defined and is displayed on the console.
The value for ‘K’ is defined and is displayed on the console.
An empty list is defined.
The list is iterated over and every element is checked to be equivalent to ‘K’.
If so, it is appended to the empty list using the ‘append’ method.
This list is displayed as the output on the console.
- Related Articles
- Python – Extract Rear K digits from Numbers
- Extract digits from Tuple list Python
- Python program to extract Keywords from a list
- Python program to extract ‘k’ bits from a given position?
- Python – Extract elements from Ranges in List
- Filter Tuples by Kth element from List in Python
- How to extract only first sub-element from a list in R?
- Python – Extract list with difference in extreme values greater than K
- List expansion by K in Python
- Python – Maximum of K element in other list
- Python – Extract Kth element of every Nth tuple in List
- How to remove an element from a list by index in Python?
- Extract numbers from list of strings in Python
- Python Program to Extract Elements from a List in a Set
- Adding K to each element in a Python list of integers

Advertisements