-3

I think this is an entry level computer science 101 course question about algorithms and data structures.

I have a list:

VAV_ID_list = ['36','38','21','29','31','25','9','13','14','19','30','8','26','6','34','11','12028','20','27','15','12032','23','16','24','37','39','12033','10']

How I can I filter out these values in VAV_ID_exclude_list from VAV_ID_list?

VAV_ID_exclude_list = ['36','38','21','29','31','25','9','13','14','19','30','8','26','6']

This code below obviously doesnt do anything any tips greatly appreciated.

filtered_VAV_ID_list = [zone for zone in VAV_ID_list if zone == 36]

print(filtered_VAV_ID_list)
6
  • list2= [zone for zone in VAV_ID_list if zone not in VAV_ID_exclude_list] Commented Jun 22, 2021 at 15:36
  • Can you post your answer @Sujay? This works ill hit the green check box Commented Jun 22, 2021 at 15:37
  • Does this answer your question? how to keep elements of a list based on another list Commented Jun 22, 2021 at 15:40
  • 1
    An "entry level computer science 101 course question" probably already has answers on Stack Overflow. How much research effort is expected of Stack Overflow users? Commented Jun 22, 2021 at 15:41
  • This has been asked many times Commented Jun 22, 2021 at 15:43

3 Answers 3

2

This is what you want

list2= [zone for zone in VAV_ID_list if zone not in VAV_ID_exclude_list]
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it in multiple ways:

This is the most straightforward way.

>>> [i for i in VAV_ID_list if i not in VAV_ID_exclude_list]
['34', '11', '12028', '20', '27', '15', '12032', '23', '16', '24', '37', '39', '12033', '10']

You can even use sets if the order is not important and you don't have duplicates.

>>> list(set(VAV_ID_list) - set(VAV_ID_exclude_list))
['24', '11', '39', '27', '20', '23', '12033', '12032', '16', '37', '34', '15', '12028', '10']

1 Comment

Thanks for the info ill hit the 1 up arrow, but @Sujay answer first
0
for el in VAV_ID_list:
    if el not in VAV_ID_exclude_list:
        print(el)

I think this will do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.