Table of Contents

Python Code to separate Even & Odd Elements of a list in 2 Separate lists.

 

Python code implementation without user-defined functions & classes

Code: 
#Python Code to separate Even & Odd Elements of a list in 2 Separate lists.

a = input("Enter the list separated by space: ").split()
b = []
for i in a:
    b.append(int(i))

c_odd_list = []
c_even_list = []

for i in b:
    if i % 2 == 0:
        c_even_list.append(i)
    else:
        c_odd_list.append(i)

print("Odd Number List is: ", c_odd_list)
print("Even Number List is: ",c_even_list)

 

Executing Python Script: 
python program.py
Enter the list separated by space: 12 34 567 1982 1933 10 12 13

 

Output: 
Odd Number List is:  [567, 1933, 13]
Even Number List is:  [12, 34, 1982, 10, 12]

 

 

Python code implementation using function

Code: 
#Python Code to separate Even & Odd Elements of a list in 2 Separate lists using function

def odd_even_list_function(arg):
    b = []
    for i in a:
        b.append(int(i))
    c_odd_list = []
    c_even_list = []
    for i in b:
        if i % 2 == 0:
            c_even_list.append(i)
        else:
            c_odd_list.append(i)
    print("Odd Number List is: ", c_odd_list)
    print("Even Number List is: ", c_even_list)

a = input("Enter the list separated by space: ").split()

#Call the Function
odd_even_list_function(a)

 

Executing Python Script: 
python program.py
Enter the list separated by space: 12 34 567 1982 1933 10 12 13

 

Output: 
Odd Number List is:  [567, 1933, 13]
Even Number List is:  [12, 34, 1982, 10, 12]

 

 

 

Python code implementation using Classes

Code: 
#Python Code to separate Even & Odd Elements of a list in 2 Separate lists using class

class Odd_even_list_class(object):
    __b = []
    def __init__(self, arg):
        self.arg = arg
        for i in self.arg:
            Odd_even_list_class.__b.append(int(i))


    def odd_list_function(self):
        c_odd_list = []
        for i in Odd_even_list_class.__b:
            if i % 2 == 0:
                c_odd_list.append(i)
        return c_odd_list

    def even_list_function(self):
        c_even_list = []
        for i in Odd_even_list_class.__b:
            if i % 2 != 0:
                c_even_list.append(i)
        return c_even_list



a = input("Enter the list separated by space: ").split()

#Create the object
Object_1 = Odd_even_list_class(a)

#Call the Function using Object Created
print("The list containing odd number is {}".format(Object_1.odd_list_function()))
print("The list containing even number is {}".format(Object_1.even_list_function()))

 

Executing Python Script: 
python program.py
Enter the list separated by space: 12 34 567 890 123 456 76 67

 

Output: 
The list containing odd number is [12, 34, 890, 456, 76]
The list containing even number is [567, 123, 67]

 

 

 

 

 

Enjoy Python Code By Pythonbaba 🙂