Your code doesn't work because the map
function returns an iterator so as you didn't iterate over the object returned by map
, the result.append
function is never called.
The "just working" version of your code would be something like:
def foo() -> list[int]: # Note that the type hint here is incorrect as you don't return anything
result: list = []
tuple(map(result.append, [x for x in range(10) if x % 2 == 0]))
print(result)
But this is very unoptimized, the version I would write would use directly the list comprehension:
def foo():
print([x for x in range(10) if x % 2 == 0])
Otherwise you could use a filter:
def foo():
print(list(filter(lambda x: x % 2 == 0, range(10))))
For more information, please read this article: https://realpython.com/python-map-function/