{"id":8211,"date":"2021-09-30T16:00:25","date_gmt":"2021-09-30T10:30:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=8211"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-put-even-and-odd-elements-in-a-list-into-two-different-lists","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-put-even-and-odd-elements-in-a-list-into-two-different-lists\/","title":{"rendered":"Python Program to Put Even and Odd elements in a List into Two Different Lists"},"content":{"rendered":"
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/p>\n Lists in Python:<\/strong><\/p>\n 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 this article, we will look at how to separate odd and even integers in a list into two separate lists.<\/p>\n When you divide a number by two, the result is an even number if the balance is zero.<\/p>\n An odd number is one that when divided by two has a remaining balance of one.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to keep even and odd elements in a given list into two separate lists some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n List Comprehension:<\/strong><\/p>\n 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 ().<\/p>\n We may simply complete the same task using list comprehension.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\n
\nIn 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 [ ].<\/p>\ngiven_list = [7, 24, 72, 39, 65, 87, 93,27, 64, 96, 82, 36, 47, 75, 12, 58, 97]<\/pre>\n
The even elements present in given list are :\r\n[24, 72, 64, 96, 82, 36, 12, 58]\r\nThe odd elements present in given list are :\r\n[7, 39, 65, 87, 93, 27, 47, 75, 97]<\/pre>\n
given_list = [55, 22, 28, 11, 98, 29, 33, 26, 73, 48, 63]<\/pre>\n
The even elements present in given list are :\r\n[22, 28, 98, 26, 48]\r\nThe odd elements present in given list are :\r\n[55, 11, 29, 33, 73, 63]<\/pre>\n
Program to Put Even and Odd elements in a List into Two Different Lists in Python<\/h2>\n
\n
Method #1:Using the append() function and traversing the list to check the elements<\/h3>\n
\n
# given list\r\ngiven_list = [7, 24, 72, 39, 65, 87, 93,\r\n 27, 64, 96, 82, 36, 47, 75, 12, 58, 97]\r\n# Create two empty lists. one for storing even numbers and the other\r\n# for storing odd numbers, referred to as evenList and oddList.\r\nevenList = []\r\noddList = []\r\n# Using a for loop, traverse the given list.\r\nfor element in given_list:\r\n # If the given list element is even then append this element to evenList\r\n if(element % 2 == 0):\r\n evenList.append(element)\r\n # If the given list element is odd then append this element to oddList\r\n else:\r\n oddList.append(element)\r\n# printing both evenList and oddList\r\nprint(\"The even elements present in given list are :\")\r\nprint(evenList)\r\nprint(\"The odd elements present in given list are :\")\r\nprint(oddList)\r\n<\/pre>\n
The even elements present in given list are :\r\n[24, 72, 64, 96, 82, 36, 12, 58]\r\nThe odd elements present in given list are :\r\n[7, 39, 65, 87, 93, 27, 47, 75, 97]<\/pre>\n
Method #2:Using List Comprehension<\/h3>\n
\n
# given list\r\ngiven_list = [7, 24, 72, 39, 65, 87, 93,\r\n 27, 64, 96, 82, 36, 47, 75, 12, 58, 97]\r\n\r\n# Take a list say evenList and use list comprehension\r\n# to store all even elements of the given list.\r\nevenList = [element for element in given_list if element % 2 == 0]\r\n# Take a list say oddList and use\r\n# list comprehension to store all odd elements of the given list.\r\noddList = [element for element in given_list if element % 2 != 0]\r\n# printing both evenList and oddList\r\nprint(\"The even elements present in given list are :\")\r\nprint(evenList)\r\nprint(\"The odd elements present in given list are :\")\r\nprint(oddList)\r\n<\/pre>\n
The even elements present in given list are :\r\n[24, 72, 64, 96, 82, 36, 12, 58]\r\nThe odd elements present in given list are :\r\n[7, 39, 65, 87, 93, 27, 47, 75, 97]<\/pre>\n
\n