Python Forum

Full Version: Writing python function difficulty
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I need to write a Python function that takes in a list of numbers, and outputs them into two lists, one consisting of even numbers, and the other odd numbers. I have managed to do a simple coding that works but when i tried to move it into a function, it doesn't work. Would appreciate your kind assistance.

import statistics 
import random
 
original_list = [random.randint(1,1000) for x in range(0,100)]
print("Original list:", original_list)

def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return even_numbers
    return odd_numbers
        
print("Odd:", oddandeven(original_list))
print("Even:", oddandeven(original_list))
The way you implement the function it returns on line 15 and line 16 never get executed. You need to return both even_numbers and odd_numbers simultaneously.

import random
  
original_list = [random.randint(1,1000) for x in range(0,100)]
print("Original list:", original_list)
 
def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return even_numbers, odd_numbers

even, odd = oddandeven(original_list)   
print("Odd: {}".format(odd))
print("Even: {}".format(even))

# you can replace above 3 lines with
print("Even: {}\nOdd: {}".format(*oddandeven(original_list)))
Like this your function would return tuple, which means even numbers will always be first element and odd numbers - second. You may want to return dict instead, which will allow to retrieve respective list by key
Hi Buran, thank you for your help. I am trying to figure out how your code works.
does this help
my_list = [1, 2, 3, 4]
  
def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return even_numbers, odd_numbers

result = oddandeven(my_list)

print(result) # print what your function returns
print(type(result)) # it's tuple
print(result[0]) # use index to access first element in the tuple
print(result[1]) # use index to access second element in the tuple
Output:
([2, 4], [1, 3]) <class 'tuple'> [2, 4] [1, 3]
I am trying to return the even_numbers when i print even_numbers, and return odd_numbers when i print odd_numbers. I think calling the index of the array may not be what my lecturer wants. I will just use your first answer instead. Thanks alot for your help.
as I said you can return also a dict
my_list = [1, 2, 3, 4]
 
def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return {'even':even_numbers, 'odd':odd_numbers}
 
result = oddandeven(my_list)
print('Even numbers: {}'.format(result['even']))
print('Odd numbers: {}'.format(result['odd']))
# instead you can use this:
print('Even numbers: {even}\nOdd numbers: {odd}'.format(**result))