

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Extract digits from Tuple list Python
- Python program to extract Keywords from a list
- List expansion by K in Python
- Python – Extract Rear K digits from Numbers
- Filter Tuples by Kth element from List in Python
- How to remove an element from a list by index in Python?
- How to extract only first sub-element from a list in R?
- Python program to extract ‘k’ bits from a given position?
- Extract numbers from list of strings in Python
- Python – Extract elements from Ranges in List
- Python Program to Extract Elements from a List in a Set
- Adding K to each element in a Python list of integers
- Python – Maximum of K element in other list
- Python Program that extract words starting with Vowel From A list
- Python – Extract list with difference in extreme values greater than K
Advertisements