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

Python – Selecting elements of list b containing elements of list a

There is a_list and b_list. We are in the process of sorting out only the b_list elements that contain elements of a_list.

a = ["Banana", "Orange", "Almond", "Kiwi", "Cabbage"]
b = [["Banana", "Pencil", "Water Bucket"], ["Orange", "Computer", "Printer"], ["Snail", "Cotton Swab", "Sweet Potato"]]
c = []

If the first element of list in b_list matches an element of list a_, this list element is put into c_list.So the desired result is

c = [["Banana", "Pencil", "Water Bucket"], ["Orange", "Computer", "Printer"]]

I’ve searched several posts, but couldn’t find an exact match, so I’m leaving a question. help

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

>Solution :

Here’s your answer

for i in b:
    if i[0] in a:
        c.append(i)
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