How do I check if all the elements of a nested list tree are identical?

I am trying to create a function allsametree(tree) that takes a list, tree, as an input and returns TRUE or FALSE if all the elements of the list are numerically identical. So far, I have the following function:

def allsametree(tree):
    if not type(tree) == list:
        return tree
    if type(tree) is list:
        final_lst = []
        for item in tree:
            final_lst.append(allsametree(item))
        return final_lst[1:] == final_lst[:-1]

While for the most part, this function works, it runs into an issue when evaluating allsametree([1, [[[[[2]]]]]])

Any tips, or alternative ways you would approach this problem?



from Recent Questions - Stack Overflow https://ift.tt/3cJPHLG
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation