Python - List Comparison

 Write a function list_compare that takes a nested list as input and returns another list with square and cubes from the input lists.


def list_compare(ls):

    out = []

    for num in ls:

        squares = list(map(lambda x: x ** 2, num))

        cubes = list(map(lambda x: x ** 3, num))

        sq_present = 'N'

        cube_present = 'N'

        for j in ls:

            if num == j:

                continue

            if j == squares:

                sq_present = 'Y'

            if j == cubes:

                cube_present = 'Y'

            if sq_present == 'Y' and cube_present == 'Y' and len(out) == 0:

                out.append(num)

                out.append(squares)

                out.append(cubes)

    if len(out) == 0:

        out = "No such list is present"

    return out

    

ls = [[1,2,3],[4,5],[1,4,9],[8,2],[1,8,27]]

res = list_compare(ls)

print(res)

No comments:

Post a Comment