Cant print lambda function string

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

message = str(filter(lambda x: x != "X", garbled))
print(message)

RESULT:

<filter object at 0x000001D0C4318208>

How do I print it as a string?
It prints the string normally in python 2, without the need to use the str() obviously.

A a filter object cannot be cast as a string, but a list.

message = list(filter(lambda x: x != "X", garbled))

Once we have the list, we may join the elements to return a string.

print (''.join(message))

In Python 2 the filter function returns a list, but in Python 3 it returns a filter object (an iterator).

2 Likes

Thanks a ton yet again mate.

1 Like