How do you list even and odd numbers in python?

In this post, you will learn different ways to write Python programs to list even and odd numbers in a list. To check whether a number is odd or even, you can either ask the user to enter a number or you can provide a list from which the program can check whether the number is odd or even. Such type of program is generally asked during interviews and in the examination. This can also be helpful to improve our programming skills.

In the given Python program, we are using the for loop to list the even and odd items in the list. In each iteration of the list, we apply the condition to check the even numbers, i.e., to check their remainder after dividing by 2. If the remainder is 0 (i% 2 == 0), add it to the even list; otherwise, add it to the odd list.

nums = [43, 20, 53, 12, 53, 5, 3, 2]

even = []
odd = []

for i in nums:
    if(i % 2 == 0):
        even.append(i)
    else:
        odd.append(i)
        
print("Even List: ",even)
print("Odd List: ",odd)

Output of the above code -

Even List:  [20, 12, 2]
Odd List:  [43, 53, 53, 5, 3]

How do you list even and odd numbers in python?

Find even and odd in list using lambda function

A lambda function is a small anonymous function that takes any number of arguments, but can only have one expression. It is useful when writing a function inside a function. In the given example, we have easily found the even and odd numbers in the list using a lambda function. The argument and the function are surrounded by parentheses.

# Check and count odd and even numbers using lambda function

# list of numbers
numberlist = [11, 30, 28, 41, 22, 85, 7]

# print Even numbers in a list using Lambda function

even_numbers = list(filter(lambda x: x % 2 == 0,numberlist))
print("Total Even numbers in the list: ", even_numbers)


# print Odd numbers in a list using Lambda function

odd_numbers = list(filter(lambda x: x % 2 == 1,numberlist))
print("Total Odd numbers in the list: ", odd_numbers)

Output of the above code -

Total Even numbers in the list:  [30, 28, 22]
Total Odd numbers in the list:  [11, 41, 85, 7]

Find even and odd in the list using while loop

This Python program for listing the even and odd numbers is the same as the above. We just replace the for loop with a while loop.

# Check and count odd and even numbers using while loop

# list of numbers
numlist = [11, 30, 28, 41, 22, 85, 7]

even_count = []
odd_count = []
num = 0
  
# using while loop     
while(num < len(numlist)):
      
    # checking condition
    if numlist[num] % 2 == 0:
        even_count.append(numlist[num])
    else:
        odd_count.append(numlist[num])
      
    # increment num 
    num += 1
      
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)

Output of the above code -

Even numbers in the list:  [30, 28, 22]
Odd numbers in the list:  [11, 41, 85, 7]

Find even and odd using function

Here is the Python program for listing the even and odd numbers from the specified range.

def find_even_odd(numbers):
    odd_numbers = [number for number in numbers if number % 2 == 1]
    even_numbers = [number for number in numbers if number % 2 == 0]
    return odd_numbers, even_numbers

numbers = range(1,40)
print("The odd numbers are {}".format(find_even_odd(numbers)[0]))
print("The even numbers are {}".format(find_even_odd(numbers)[1]))

Output of the above code:

The odd numbers are [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]
The even numbers are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]

Convert Python list to numpy array
Convert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary
Python program to print all even numbers between 1 to 100
Python program to multiply two numbers
Program to find area of triangle in Python
Python program to sort words in alphabetical order

How do you print even and odd numbers in a list Python?

Python Program to Print Even and Odd Numbers in a List.
num_list=[].
n=int(input("Enter the Starting of the range:")).
k=int(input("Enter the Ending of the range:")).
for i in range(n,k):.
num_list. append(i).
print("Original Number List:", num_list).
even_list=[].
odd_list=[].

How do you check if a list is even or odd in Python?

Python Code: num = int(input("Enter a number: ")) mod = num % 2 if mod > 0: print("This is an odd number. ") else: print("This is an even number. ")

How do I get the even numbers in a list Python?

Python program to print even numbers in a list.
Using filter & lambda function list1 = [11, 23, 45, 23, 64, 22, 11, 24] even_no = list(filter(lambda x: (x % 2 == 0), list1)) print(even_no) ... .
Using list comprehension list1 = [11, 23, 45, 23, 64, 22, 11, 24] even_nos = [num for num in list1 if num % 2 == 0] print(even_nos).

How do you add odd numbers in a list in Python?

Practical Data Science using Python We have to find sum of all odd elements from the list. So, if the input is like nums = [5,7,6,4,6,9,3,6,2], then the output will be 24 because 5+7+9+3 = 24. return the sum of elements in l by passing l into sum() function.