Python – Odd elements removal in List
Last Updated :
09 Apr, 2023
Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge of it is a must. Lets discuss certain ways in which removal of odd values is achieved.
Method #1 : Naive Method In naive method, we iterate through whole list and append all the filtered, non odd values into a new list, hence ready to be performed with subsequent operations.
Python3
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ("The original list is : " + str (test_list))
res = []
for val in test_list:
if not (val % 2 ! = 0 ) :
res.append(val)
print (" List after removal of Odd values : " + str (res))
|
Output :
The original list is : [1, 9, 4, 7, 6, 5, 8, 3]
List after removal of Odd values : [4, 6, 8]
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using list comprehension The longer task of using the naive method and increasing line of codes can be done in compact way using this method. We just check for non odd values and construct the new filtered list.
Python3
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ("The original list is : " + str (test_list))
res = [i for i in test_list if not (i % 2 ! = 0 )]
print (" List after removal of odd values : " + str (res))
|
Output :
The original list is : [1, 9, 4, 7, 6, 5, 8, 3]
List after removal of Odd values : [4, 6, 8]
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method#3: Using Recursive Method
Python3
def remove_odd(lst,newlst = [],start = 0 ):
if start = = len (lst):
return newlst
if lst[start] % 2 = = 0 :
newlst.append(lst[start])
return remove_odd(lst,newlst,start + 1 )
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ( 'The original list is : ' + str (test_list))
res = remove_odd(test_list)
print ( 'List after removal of odd values : ' + str (res))
|
Output
The original list is : [1, 9, 4, 7, 6, 5, 8, 3]
List after removal of odd values : [4, 6, 8]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4: Using filter():
Python3
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
result = list ( filter ( lambda x: x % 2 = = 0 , test_list))
print ( 'The original list is : ' + str (test_list))
print ( "List after removal of odd values: " , result)
|
Output
The original list is : [1, 9, 4, 7, 6, 5, 8, 3]
List after removal of odd values: [4, 6, 8]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using numpy
- Define a list of integers called “test_list”.
- Convert the list to a numpy array using the “np.array” function.
- Use boolean indexing to filter the array and keep only the even values: create a boolean mask by
- applying the modulo operator (%) to the array and checking if each element is equal to zero. Use this mask to select only the elements of the array that are even.
- Convert the resulting numpy array back to a list using the “tolist” method.
- Print the new list.
Python3
import numpy as np
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ( 'The original list is : ' + str (test_list))
arr = np.array(test_list)
res = arr[arr % 2 = = 0 ].tolist()
print ( "List after removal of odd values: " + str (res))
|
Output
The original list is : [1, 9, 4, 7, 6, 5, 8, 3]
List after removal of odd values: [4, 6, 8]
The time complexity of this algorithm is O(n), where n is the length of the input list. This is because the algorithm needs to iterate over every element of the input list once in order to create the numpy array, filter the array using boolean indexing, and convert the resulting array back to a list.
The auxiliary space complexity of this algorithm is O(n), where n is the number of even values in the input list. This is because the algorithm creates a new numpy array containing only the even values, and the size of this array is proportional to the number of even values in the input list.
Share your thoughts in the comments
Please Login to comment...