How do you remove an odd number from a list in Python?

First Approach

# --- Naive Approach Brute Force def remove_odd[array]: """Very unefficient Funciton, it has to copy the arr then iterate through it, then calls remove function, however it does the job, good for small Lists""" array_copy = array.copy[] for n in array_copy: if n % 2 != 0: array.remove[n] return array tests = [[4,5,4], [4,5,4,7,9,11], [4,5,4,7,9,11,12,13]] for test in tests: print[f'List Before --> {test}'] result = remove_odd[test] print[f'List After --> {test}'] print['===='*15] ## Other Solution ## def remove_odd[array]: """Better Solution, it iterates through the Array once""" idx = 0 offset = 0 # NOTE: Offset keeps tracks of the jumps after each iteration max_iter = len[array] while max_iter: n = array[idx-offset] if n % 2 != 0: offset += 1 array.remove[n] idx += 1 max_iter -= 1 tests = [[4,5,4], [4,5,4,7,9,11], [4,5,4,7,9,11,12,13]] for test in tests: print[f'List Before --> {test}'] result = remove_odd[test] print[f'List After --> {test}'] print['===='*15]

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 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]

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 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]




Article Tags :

Python

Python Programs

Python list-programs

Read Full Article

Python Program to Remove Odd or Even Numbers From a List

If you are reading this article, I am sure you must be knowing what odd and even numbers are. An odd number is a number that does not give 0 as a remainder when divided by 2, and an even number is a number that gives 0 as a remainder when divided by 2. We can use the same logic to write a Python program to remove odd or even numbers from a list.

Below is how we can define a Python function to remove all the odd numbers from a Python list:

View this gist on GitHub

[12]

Just like the function defined above, below is how we can define a Python function to remove all the even numbers from a Python list:

View this gist on GitHub

[15, 7, 9]

Usually, in a Python coding interview, you might get this question as; how to write a Python program or define a Python function to remove all odd or even values from a Python list.

Summary

So this is how you can write a Python program or define a Python function to remove all the odd or even values from a list. This list in Python is one of the most used built-in data structures, so during a Python coding interview, you may come across many questions based on Python lists. I hope you liked this article on how to write a Python program to remove odd or even values from a list. Feel free to ask your valuable questions in the comments section below.

Python print list after removing the ODD numbers

19th December 2019 by

Here, we are going to actualize a python program that will print the list in the wake of expelling ODD numbers.

Given a list, and we need to print the list in the wake of expelling the ODD numbers in Python.

Example:

Input: list = [11, 22, 33, 44, 55] Output: list after removing ODD numbers list = [22, 44]

Rationale:

  • Navigate each number in the list by utilizing for…in circle.
  • Check the condition for example checks number is separable by 2 or not – to check ODD, the number must not be distinguishable by 2.
  • On the off chance that number isn’t distinct by 2 for example ODD number from the list, use list.remove[] strategy.

Program:

# list with EVEN and ODD number list = [11, 22, 33, 44, 55] # print original list print "Original list:" print list # loop to traverse each element in the list # and, remove elements # which are ODD [not divisible by 2] for i in list: if[i%2 != 0]: list.remove[i] # print list after removing ODD elements print "list after removing ODD numbers:" print list

Output:

Original list: [11, 22, 33, 44, 55] list after removing ODD numbers: [22, 44]

Tags Python print list after removing the ODD numbersPost navigation

Python print list after removing EVEN numbers

Python Input comma-separated elements, convert into list and print

remove odd numbers from linked list in python

Python program for remove odd numbers from linked list. Here problem description and other solutions.

# Python 3 program for # Delete odd nodes from linked list # Node of Linked List class LinkNode : def __init__[self, data] : # Set node value self.data = data self.next = None class SingleLL : def __init__[self] : self.head = None self.tail = None # Add new node at the end of linked list def addNode[self, value] : # Create a new node node = LinkNode[value] if [self.head == None] : self.head = node else : self.tail.next = node self.tail = node # Display linked list element def display[self] : if [self.head == None] : return temp = self.head # iterating linked list elements while [temp != None] : print[temp.data, end = " → "] # Visit to next node temp = temp.next print["null"] # Delete all odd key nodes in linked list def deleteOddNodes[self] : # Define some auxiliary variables current = self.head auxiliary = None back = None # iterating linked list elements while [current != None] : if [current.data % 2 != 0] : # When get odd node auxiliary = current else : back = current # Visit to next node current = current.next if [auxiliary != None] : # When Deleted node exists if [back == None] : # When front node is odd node # head visit to next node self.head = current else : # When deleting centralized node back.next = current if [self.tail == auxiliary] : # When delete last node # Set new tail self.tail = back # Unlink deleted node auxiliary.next = None auxiliary = None def main[] : sll = SingleLL[] # Add linked list node sll.addNode[3] sll.addNode[1] sll.addNode[4] sll.addNode[7] sll.addNode[9] sll.addNode[6] sll.addNode[5] sll.addNode[11] # Before effect print["Before Delete Odd Key Nodes"] # 3 → 1 → 4 → 7 → 9 → 6 → 5 → 11 → NULL sll.display[] # Perform delete operation sll.deleteOddNodes[] # After effect print["After Delete Odd Key Nodes"] # 4 → 6 → NULL sll.display[] if __name__ == "__main__": main[]

Output

Before Delete Odd Key Nodes 3 → 1 → 4 → 7 → 9 → 6 → 5 → 11 → null After Delete Odd Key Nodes 4 → 6 → null

Python Program to Remove Even Numbers in a List using the While Loop

# Remove Even index List Items evenList = [] listNumber = int[input["Enter the Total List Items = "]] for i in range[1, listNumber + 1]: listValue = int[input["Enter the %d List Item = " %i]] evenList.append[listValue] print["List Items = ", evenList] i = 0 while [i < len[evenList]]: if [evenList[i] % 2 == 0]: evenList.remove[evenList[i]] i = i + 1 print["List Items after removing even Items = ", evenList]

Python remove even numbers from a list using a while loop output

Enter the Total List Items = 6 Enter the 1 List Item = 21 Enter the 2 List Item = 98 Enter the 3 List Item = 7 Enter the 4 List Item = 32 Enter the 5 List Item = 19 Enter the 6 List Item = 32 List Items = [21, 98, 7, 32, 19, 32] List Items after removing even Items = [21, 7, 19]

In this Python example, we used the list comprehension to remove or delete the even numbers from the given list.

# Remove Even index List Items evenList = [] listNumber = int[input["Enter the Total List Items = "]] for i in range[1, listNumber + 1]: listValue = int[input["Enter the %d List Item = " %i]] evenList.append[listValue] print["List Items = ", evenList] evenList = [ev for ev in evenList if ev % 2 != 0] print["List Items after removing even Items = ", evenList]

Remove even numbers from a list using List comprehension output

Enter the Total List Items = 3 Enter the 1 List Item = 11 Enter the 2 List Item = 22 Enter the 3 List Item = 33 List Items = [11, 22, 33] List Items after removing even Items = [11, 33]

This Python program used the list, filter, and lambda Functions to remove the even numbers.

# Remove Even index List Items evenList = [] listNumber = int[input["Enter the Total List Items = "]] for i in range[1, listNumber + 1]: listValue = int[input["Enter the %d List Item = " %i]] evenList.append[listValue] print["List Items = ", evenList] oddList = list[filter[lambda x : [x % 2 != 0], evenList]] print["List Items after removing even Items = ", oddList]

Remove List even numbers output

Enter the Total List Items = 4 Enter the 1 List Item = 21 Enter the 2 List Item = 42 Enter the 3 List Item = 99 Enter the 4 List Item = 56 List Items = [21, 42, 99, 56] List Items after removing even Items = [21, 99]

Video liên quan

Bài mới nhất

Chủ Đề