Python Program to Put Even and Odd elements in a List into Two Different Lists

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Lists in Python:

The majority of applications do not merely deal with variables. Lists of variables are also used. For example, a program may use the list of students from a keyboard or a file to handle information about students in a class. No modification of program source code shall be required to adjust the number of students in the classes.
In Python, the data structure named list can be used for the saving of this data (the term “array” is used in other programming languages). A list is like characters in string a succession of elements numbers 0. The list can be manually set by enumerating the list’s elements in square brackets [ ].

In this article, we will look at how to separate odd and even integers in a list into two separate lists.

When you divide a number by two, the result is an even number if the balance is zero.

An odd number is one that when divided by two has a remaining balance of one.

Examples:

Example1:

Input:

given_list = [7, 24, 72, 39, 65, 87, 93,27, 64, 96, 82, 36, 47, 75, 12, 58, 97]

Output:

The even elements present in given list are :
[24, 72, 64, 96, 82, 36, 12, 58]
The odd elements present in given list are :
[7, 39, 65, 87, 93, 27, 47, 75, 97]

Example2:

Input:

given_list = [55, 22, 28, 11, 98, 29, 33, 26, 73, 48, 63]

Output:

The even elements present in given list are :
[22, 28, 98, 26, 48]
The odd elements present in given list are :
[55, 11, 29, 33, 73, 63]

Program to Put Even and Odd elements in a List into Two Different Lists in Python

There are several ways to keep even and odd elements in a given list into two separate lists some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Using the append() function and traversing the list to check the elements

Approach:

  • Scan the given list or give list input as static.
  • Create two empty lists. one for storing even numbers and the other for storing odd numbers referred to as evenList and oddList.
  • Using a for loop, traverse the given list.
  • Using the modulus operator, determine whether the element is even or odd.
  • If the given list element is even then append this element to evenList
  • If the given list element is odd then append this element to oddList
  • As a result, the even and odd items are divided into two independent lists.
  • Both the evenList and the oddList should be printed.

Below is the implementation:

# given list
given_list = [7, 24, 72, 39, 65, 87, 93,
              27, 64, 96, 82, 36, 47, 75, 12, 58, 97]
# Create two empty lists. one for storing even numbers and the other
# for storing odd numbers, referred to as evenList and oddList.
evenList = []
oddList = []
# Using a for loop, traverse the given list.
for element in given_list:
    # If the given list element is even then append this element to evenList
    if(element % 2 == 0):
        evenList.append(element)
    # If the given list element is odd then append this element to oddList
    else:
        oddList.append(element)
# printing both evenList and oddList
print("The even elements present in given list are :")
print(evenList)
print("The odd elements present in given list are :")
print(oddList)

Output:

The even elements present in given list are :
[24, 72, 64, 96, 82, 36, 12, 58]
The odd elements present in given list are :
[7, 39, 65, 87, 93, 27, 47, 75, 97]

Method #2:Using List Comprehension

List Comprehension:

Set-builder notation, commonly known as set comprehension, is a notion in mathematics. Python provides list comprehensions, which are inspired by this notion. In fact, Python list comprehension is one of the language’s distinguishing characteristics. It enables us to write simple, understandable code that outperforms uglier alternatives like as for loops or map functions ().

We may simply complete the same task using list comprehension.

Approach:

  • Scan the given list or give list input as static.
  • Take a list say evenList and use list comprehension to store all even elements of the given list.
  • Take a list say oddList and use list comprehension to store all odd elements of the given list.
  • As a result, the even and odd items are divided into two independent lists.
  • Both the evenList and the oddList should be printed.
  • The Exit of the Program.

Below is the implementation:

# given list
given_list = [7, 24, 72, 39, 65, 87, 93,
              27, 64, 96, 82, 36, 47, 75, 12, 58, 97]

# Take a list say evenList and use list comprehension
# to store all even elements of the given list.
evenList = [element for element in given_list if element % 2 == 0]
# Take a list say oddList and use
# list comprehension to store all odd elements of the given list.
oddList = [element for element in given_list if element % 2 != 0]
# printing both evenList and oddList
print("The even elements present in given list are :")
print(evenList)
print("The odd elements present in given list are :")
print(oddList)

Output:

The even elements present in given list are :
[24, 72, 64, 96, 82, 36, 12, 58]
The odd elements present in given list are :
[7, 39, 65, 87, 93, 27, 47, 75, 97]

Related Programs:

Leave a Comment