Ceefar
Ceefar

Reputation: 86

Why is Map function not computing my lambda?

So I'm trying to use the map function with a lambda to write each item of a list to a txt file on a new line

map(lambda x: text_file.write(f"{x}\n"), itemlist_with_counts_formatted)

I understand that map returns a map object, but I don't need the return value.

What I want is for the map function to compute the lambda, which adds "\n" to the end of each item in the given list.

I thought that map should do this (compute the function (lambda appends "\n") using arguments from the iterable) but nothing gets output to the txt file.

For clarity, I can totally do this with a list comprehension but I wanted to learn how to use map (and properly anonymous lambdas), so am looking for help solving it using these two functions specifically (if possible).

map(lambda x: text_file.write(f"{x}\n"), itemlist_with_counts_formatted)

I have also tried it without the f string, using just x + "\n" but this doesn't work either

Yes the txt file is open, and yes I can get it to work using other methods, the problem is exclusive to how I'm using map or how I'm using lambda, which must be wrong in some way. I've been doing this for 6 weeks so its probably something stupid but I've tried to figure it out myself and i just can't and I've checked but can't find anything on here - appreciate any help I can get.

Upvotes: 1

Views: 91

Answers (2)

Adam Viscusi
Adam Viscusi

Reputation: 69

i think that the problem is that this use of the map function is a bit unproper. As said in the documentation the map function returns a generator for the results iterable, while the write function is not returning anything. This might brake something during the map internals.

I'd suggest you to use map only to add line end and then use the writeline function on the resulting generator, something like:

text_file.writelines(map(lambda x: f"{x}\n", itemlist_with_counts_formatted))

(Not tested)

Upvotes: 0

mozway
mozway

Reputation: 260825

You should really not use map for this task.

It looks fancy, but this is the same as using list comprehensions for side effects. It's considered bad practice.

[print(i) for i in range(3)]

Which should be replaced with:

for i in range(3):
    print(i)

In you case, use:

for item in itemlist_with_counts_formatted:
    text_file.write(f"item\n")

why your code did not work:

map returns a generator, nothing is evaluated until something consumes the generator. You would need to do:

list(map(lambda x: text_file.write(f"{x}\n"), itemlist_with_counts_formatted))

But, again, don't, this is useless, less efficient and less explicit.

But I really want a one-liner!

Then use:

text_file.write('\n'.join(itemlist_with_counts_formatted))

NB. unlike the other alternatives in this answer, this one does not add a trailing '\n' in the end of the file.

I really, really, want to use map:

text_file.writelines(map(lambda x: f'{x}\n', itemlist_with_counts_formatted))

Upvotes: 4

Related Questions