Write a Python Program to Put Even And Odd Elements in a List Into Two Different Lists
Let's take a new variable named length and ask the user to enter the list length.
Take another variable numbers_list and initialize it to an empty list. We have to fill this list with the list of numbers by iterating from zero to n (list length) and keep asking the user to enter a number using an Input function call inside the loop.
Take another variable numbers_list and initialize it to an empty list. We have to fill this list with the list of numbers by iterating from zero to n (list length) and keep asking the user to enter a number using an Input function call inside the loop.
Once we get the the list of integers we have to append it using the list inbuilt function append to the list
length = int(input("Enter a List Length : "))
numbers_list = []
for _ in range(length):
temp = int(input("Enter a List Item : "))
numbers_list.append(temp)
As we know the definition of even numbers is that a number is completely divisible by 2 is referred to as an even number.
The number that is not completely divisible by 2 is referred to as odd numbers. Let's use a list comprehension method to get the given list and order list of numbers.
Iterate all the items using for loop and check if if item is completely divisible by 2 you inside the list comprehension this will give us the even list
Iterate all the items using for loop and check if item is not completely divisible by 2 you inside the list comprehension this will give us the odd number list
even_list = [item for item in numbers_list if item % 2 == 0]
odd_list = [item for item in numbers_list if item % 2 != 0]
The number that is not completely divisible by 2 is referred to as odd numbers. Let's use a list comprehension method to get the given list and order list of numbers.
Iterate all the items using for loop and check if if item is completely divisible by 2 you inside the list comprehension this will give us the even list
Iterate all the items using for loop and check if item is not completely divisible by 2 you inside the list comprehension this will give us the odd number list
even_list = [item for item in numbers_list if item % 2 == 0]
odd_list = [item for item in numbers_list if item % 2 != 0]
Once we get the even list and odd number list of numbers print the list using the print function call
print("Even List is : ", even_list)
print("Odd List is : ", odd_list)
Final program
============================
length = int(input("Enter List Length : "))
numbers_list = []
for _ in range(length):
temp = int(input("Enter List Element : "))
numbers_list.append(temp)
even_list = [item for item in numbers_list if item % 2 == 0]
odd_list = [item for item in numbers_list if item % 2 != 0]
print("Even List is : ", even_list)
print("Odd List is : ", odd_list)
Output
================
Enter List Length : 10
Enter List Element : 34
Enter List Element : 45
Enter List Element : 23
Enter List Element : 21
Enter List Element : 9
Enter List Element : 78
Enter List Element : 85
Enter List Element : 44
Enter List Element : 55
Enter List Element : 78
Even List is : [34, 78, 44, 78]
Odd List is : [45, 23, 21, 9, 85, 55]
Conclusion
=================
Run the program by yourself by providing the random number as input and get the even list and odd list Divided. comment down below if you have any suggestions
0 Comments