Python Program to Put Even and Odd Elements of a List into Two Different Lists
Hey There,
In this blog, I have shown how to put even an odd elements of a list into two different lists.
Consider the given list as : list = [1,2,3,4,5,6,7,8,9]
Take list for even elements : even_list = [ ]
Take list for odd elements = odd_list = [ ]
To check each element in the given list we will use for loop, that can be written as : for i in list:
To know a element is even, we divide it by 2. If element remainder is zero then it is a even number. That can be written as : if i%2 == 0:
If that element is divisible by 2, then we add that element to the even_list and can be written as : even_list.append( i )
If it is not divisible by 2, then we append it to odd_list : odd_list.append( i )
So, the program can be written as :
list = [1,2,3,4,5,6,7,8,9]
even_list = [ ]
odd_list = [ ]
for i in list:
if i%2==0:
even.list.append(i)
else:
odd_list.append(i)
To check each element in the given list we will use for loop, that can be written as : for i in list:
To know a element is even, we divide it by 2. If element remainder is zero then it is a even number. That can be written as : if i%2 == 0:
If that element is divisible by 2, then we add that element to the even_list and can be written as : even_list.append( i )
If it is not divisible by 2, then we append it to odd_list : odd_list.append( i )
So, the program can be written as :
list = [1,2,3,4,5,6,7,8,9]
even_list = [ ]
odd_list = [ ]
for i in list:
if i%2==0:
even.list.append(i)
else:
odd_list.append(i)
Output :
Python Program to Put Even and Odd Elements of a List into Two Different Lists |
Comments
Post a Comment