Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Odd elements removal in List

Python – Odd elements removal in List

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




# Python3 code to demonstrate
# Odd elements removal in List
# using naive method
 
# initializing list
test_list = [1, 9, 4, 7, 6, 5, 8, 3]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using naive method
# Odd elements removal in List
res = []
for val in test_list:
    if not (val % 2 != 0) :
        res.append(val)
 
# printing result
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




# Python3 code to demonstrate
# Odd elements removal in List
# using list comprehension
 
# initializing list
test_list = [1, 9, 4, 7, 6, 5, 8, 3]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list comprehension
# Odd elements removal in List
res = [i for i in test_list if not (i % 2 != 0)]
 
# printing result
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




# Python3 code to demonstrate
# Odd elements removal in List
#Using Recursive Method
 
 
def remove_odd(lst,newlst=[],start=0):
  if start==len(lst): #base condition
    return newlst
  if lst[start]%2==0: #checking if no is even or not
    newlst.append(lst[start])
  return remove_odd(lst,newlst,start+1)
# initializing list
test_list = [1, 9, 4, 7, 6, 5, 8, 3]
 
# printing original list
print ('The original list is : ' + str(test_list))
 
#Using Recursive Method
# Odd elements removal in List
res = remove_odd(test_list)
 
# printing result
print ('List after removal of odd values : ' + str(res))
 
#this code contributed by tvsk


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))
# printing original list
print ('The original list is : ' + str(test_list))
print("List after removal of odd values: ", result)
#This code is contributed by Jyothi  pinjala.


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

  1. Define a list of integers called “test_list”.
  2. Convert the list to a numpy array using the “np.array” function.
  3. Use boolean indexing to filter the array and keep only the even values: create a boolean mask by 
  4. 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.
  5. Convert the resulting numpy array back to a list using the “tolist” method.
  6. Print the new list.

Python3




import numpy as np
 
test_list = [1, 9, 4, 7, 6, 5, 8, 3]
# printing original list
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))
#This code is contributed by Vinay pinjala.


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. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments