Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split list into two lists, odd and even, using a lambda

This is what I have:

ev_filt = filter(lambda x: x % 2 == 0, list1)
od_filt = filter(lambda x: x % 2 != 0, list1)

ev_list = list(ev_filt)
od_list = list(od_filt)

length = int(input("Enter the number of words yer gon pass"))

# initialize the list using for loop
for i in range(0, length):
   item = int(input("Pass a number bro" + str(i+1) + " :"))
   list1.append(item)
   
print(ev_list)
print(od_list)

I’ve tried to continue with this template, but it won’t work.

Why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I solve this?

>Solution :

As I mentioned in my comment, code runs from top to bottom in order. You sort your list1 into odd and even before it’s initialized and before there are values in the list. Switch the order and you should be golden:

length = int(input("Enter the number of words yer gon pass"))

# initialize the list using for loop
list1=[]
for i in range(0, length):
   item = int(input("Pass a number bro" + str(i+1) + " :"))
   list1.append(item)

ev_filt = filter(lambda x: x % 2 == 0, list1)
od_filt = filter(lambda x: x % 2 != 0, list1)

ev_list = list(ev_filt)
od_list = list(od_filt)

   
print(ev_list)
print(od_list)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading