- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Put Even and Odd elements in a List into Two Different Lists
When it is required to place even and odd elements in a list into two different lists, a method with two empty lists can be defined. The modulus operator can be used to determine if the number is even or odd.
Below is the demonstration of the same −
Example
def split_list(my_list): even_list = [] odd_list = [] for i in my_list: if (i % 2 == 0): even_list.append(i) else: odd_list.append(i) print("The list of odd numbers are :", even_list) print("The list of even numbers are :", odd_list) my_list = [2, 5, 13, 17, 51, 62, 73, 84, 95] print("The list is ") print(my_list) split_list(my_list)
Output
The list is [2, 5, 13, 17, 51, 62, 73, 84, 95] The list of odd numbers are : [2, 62, 84] The list of even numbers are : [5, 13, 17, 51, 73, 95]
Explanation
A method named ‘split_list’ is defined, that takes a list as a parameter.
Two empty lists are defined.
The parameter list is iterated over, and the modulus operator is used to determine if the number is even or odd.
If it is an even number, it is added to first list, otherwise it is added to the second list.
This is displayed as output on the console.
Outside the function, a list is defined, and the method is called by passing this list.
The output is displayed on the console.
- Related Articles
- Python program to split the even and odd elements into two different lists.
- Java program to split the Even and Odd elements into two different lists
- Python program to Count Even and Odd numbers in a List
- C# program to split the Even and Odd integers into different arrays
- Program to interleave list elements from two linked lists in Python
- Swap Even Index Elements And Odd Index Elements in Python
- Odd Even Linked List in Python
- Python program to convert a list into a list of lists using a step value
- Python program to list the difference between two lists.
- Program to find sum of odd elements from list in Python
- Golang Program to Print the Largest Even and Largest Odd Number in a List
- Convert list into list of lists in Python
- Program to find only even indexed elements from list in Python
- C program to store even, odd and prime numbers into separate files
- Program to find minimum difference between two elements from two lists in Python

Advertisements