Jason Jason - 22 days ago 7
Python Question

Combining two lists into one list where they share the same values and removing duplicates using list comprehensions

I am trying to combine two lists:
One holds Square numbers.
The other stores Pentagonal Numbers.

def pentaSquares():
l = []
n = 0
squares = lambda x: [x*x for x in range(n)]
penta = lambda y: [y*(3*y-1)//2 for y in range(n)]
while l.index < 4:
l = [i for i in squares for j in penta if squares == penta]
n = n+1
return l


I must merge these lists using List Comprehensions where their values match until there are 4 elements in the list.

If somebody could point me in the right direction, that would be much appreciated.

I am currently getting this error:
TypeError: unorderable types: builtin_function_or_method() < int()

Answer
def pentaSquares(n):
    squarlist=[]
    pentalist=[]
    squares = lambda x:x*x
    penta = lambda y:y*(3*y-1)//2
    for i in range(n):
        squarlist.append(squares(i))
        pentalist.append(penta(i))
    l=[x for x in squarlist if x in pentalist]
    return l

>>> pentaSquares(10000)
[0, 1, 9801, 94109401]
Comments