[Tutor] Appropriate use of assignment expression

Manprit Singh manpritsinghece at gmail.com
Sat Nov 20 06:52:08 EST 2021


Dear sir,

Consider a problem of list of numbers as given below :

ls = [234, 5445, 613, 959, 3441, 212, 239]

Now i only need those numbers from the list ls which are palindrome, into a
new list, doing this way gives the result :

ans = [ele for ele in ls if (y:=str(ele))==y[::-1]]
print(ans)    # Gives result given below which is the right answer
[5445, 959, 212]

Is it ok to use assignment expressions in this way ?

Regards
Manprit Singh


More information about the Tutor mailing list