Python – Extract element from list succeeded by K
Last Updated :
10 May, 2023
Given a list, extract the elements which are having K as the next element.
Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5], K = 3
Output : [2, 5]
Explanation : Elements before 3 are 2, 5.
Input : test_list = [2, 3, 5, 7, 8, 5, 3, 8], K = 8
Output : [7, 3]
Explanation : Elements before 8 are 7, 3.
Method #1: Using loop
In this, we iterate the list and look for each K, and extract the element preceding it.
Python3
test_list = [ 2 , 3 , 5 , 7 , 8 , 5 , 3 , 5 ]
print ( "The original list is : " + str (test_list))
K = 5
res = []
for idx in range ( len (test_list) - 1 ):
if test_list[idx + 1 ] = = K:
res.append(test_list[idx])
print ( "Extracted elements list : " + str (res))
|
Output
The original list is : [2, 3, 5, 7, 8, 5, 3, 5]
Extracted elements list : [3, 8, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
Another way to solve this question, in this, we use list comprehension as shorthand to solve the problem of getting elements.
Python3
test_list = [ 2 , 3 , 5 , 7 , 8 , 5 , 3 , 5 ]
print ( "The original list is : " + str (test_list))
K = 5
res = [test_list[idx]
for idx in range ( len (test_list) - 1 ) if test_list[idx + 1 ] = = K]
print ( "Extracted elements list : " + str (res))
|
Output
The original list is : [2, 3, 5, 7, 8, 5, 3, 5]
Extracted elements list : [3, 8, 3]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method 3: Using the enumerate() function.
Step-by-step approach:
- Initialize the original list and K value.
- Create an empty list to store the extracted elements.
- Loop through the list using the enumerate() function to access both the index and value of each element in the list.
- Check if the current element is not the last element of the list and if the next element is equal to K.
- If the condition is true, append the current element to the extracted elements list.
- After the loop, print the extracted elements list.
Python3
test_list = [ 2 , 3 , 5 , 7 , 8 , 5 , 3 , 5 ]
print ( "The original list is : " + str (test_list))
K = 5
res = []
for idx, val in enumerate (test_list):
if idx < len (test_list) - 1 and test_list[idx + 1 ] = = K:
res.append(val)
print ( "Extracted elements list : " + str (res))
|
Output
The original list is : [2, 3, 5, 7, 8, 5, 3, 5]
Extracted elements list : [3, 8, 3]
Time complexity: O(n) as it involves looping through the list once.
Auxiliary space: O(k), where k is the number of elements succeeded by K in the given list.
Method #4: Using slicing and zip function
Step-by-step approach:
- Start by initializing the test_list and K as given in the problem statement.
- Create a new list called “res” to store extracted elements.
- Use slicing to create a new list called “next_list” that contains all elements in the original list except the first element.
- Use the zip function to create a tuple of each pair of elements in the original list and “next_list”.
- Loop through each tuple and check if the second element in the tuple is equal to K.
- If it is, append the first element of the tuple to the “res” list.
- Finally, print the “res” list to display the extracted elements.
Python3
test_list = [ 2 , 3 , 5 , 7 , 8 , 5 , 3 , 5 ]
K = 5
res = []
next_list = test_list[ 1 :]
for x, y in zip (test_list, next_list):
if y = = K:
res.append(x)
print ( "Extracted elements list : " + str (res))
|
Output
Extracted elements list : [3, 8, 3]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1), since we only use a few variables to store information about the list.
Share your thoughts in the comments
Please Login to comment...