Filter list inside list

Asked

Viewed 29 times

-1

My question is how to compare my list and filter it. The case would be if, the data entry on my list contains a list, I separate this list. I tried by type(list) but returned nothing. I appreciate the strength!

class SearchList():

    def __init__(self, insert_list):
        self.insert = insert_list

    def exec_list(self):
        for i in self.insert:
            if i == type(list):
                print(i)


my_list = ["a", "b", 1, 2, 3, ["x1", "x2", 4.7]]

prt_list = SearchList(my_list)
print(prt_list.exec_list())
  • 4

    if i == type(list), here you are comparing the Valor of i with the guy of list, That doesn’t make any sense. Why not do something like type(i) == list? It wouldn’t make more sense?

  • Wouldn’t it be easier to do filter(lambda e: isinstance(e, list), my_list)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.