- 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 Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
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)
The list is : [45, 65, 32, 78, 99, 10, 21, 2] The value of K is 99 The result is : 
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