1
elements = ['121', '9', '55', '5']

I am trying to check if any of the items in elements list is a palindromic number. If there exists any, return True(else False). I tried implementing by using map + lambda: here is the snippet,

print(any(map(lambda x: (all(map(lambda y: x[y] == x[-y-1], range(int(len(x)/2))))), elements )))

but I couldn't implement the same idea using list comprehension technique. Can someone please, suggest me with it. Here is what i did:

print(any([True if x[y] == x[-y-1] for y in (range(int(len/2)) for x in elements)]))
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Rajesh M
  • 31
  • 7

4 Answers4

1

You still have to use an all(..) otherwise you will return True from the moment one character is palindromic in a string:

any([all(x[y] == x[-y-1] for y in range(len(x)//2)) for x in elements])

Or you can decide to omit the all(..), and work with slices:

any([x[:len(x)//2] == x[:len(x)//2-1:-1] for x in elements])

We do not need to stop at len(x)//2 and use a substring for the reverse: we might decide to compare x with x[::-1] but since our strings are half as long, the comparison will take less long.

Furthermore you better use // for integer division: it will floor the result.

That being said using any without the list comprehension will usually be faster: by using list comprehension you force Python to first evaluate all the elements and check if these are a palindrom. Then you are going to check whether one was. By using a generator, Python will stop from the moment it has found a single palindrome, not looking whether the other elements are palindromes.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

This checks whether the number in a list is a palindrome or not.
This includes a function and an iterable.

def palindrome(n):
    x1 = list(str(n))
    x2 = x1[::-1]
    if x1 == x2 :
        return True
    else:
        return False

elements ['121','9','55', '5']
list = [palindrome(int(n)) for n in elements]
Adriaan
  • 17,741
  • 7
  • 42
  • 75
David Gladson
  • 547
  • 9
  • 17
  • 1
    Please provide some context and explanation for your code. A code snippet alone does not do a good job explaining *why* your answer works. – Ben Visness Jul 19 '17 at 16:49
  • Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Oct 07 '22 at 06:37
0

I've implemented lambda functions to define both sides:

elements = ['121', '9', '55', '5', '123']

count_to_middle = lambda x: len(x) // 2

left_side = lambda x: list(x[:count_to_middle(x)])
right_side = lambda x: list(reversed(x))[:count_to_middle(x)]

array = [left_side(var) == right_side(var) for var in elements]

print array
# >>> [True, True, True, True, False]

print all(array)
# >>> False

print any(array)
# >>> True

This should help.

foxyblue
  • 2,859
  • 2
  • 21
  • 29
0

In your code line:

print(any([True if x[y] == x[-y-1] for y in (range(int(len/2)) for x in elements)]))

You use range(int(len/2)) without specifying it (len(of_somnething))

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
David Gladson
  • 547
  • 9
  • 17