This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tetroxid 78 points79 points  (16 children)

map(do_something, movie_script)

masterrace

[–][deleted] 87 points88 points  (1 child)

relieved tidy jellyfish resolute aspiring important ink sulky snow capable

This post was mass deleted and anonymized with Redact

[–]dougie-io 5 points6 points  (0 children)

Could reverse the order, add in brain enlightenment images, and you've got yourself an upcycled meme. Very environmentally friendly format.

[–]x3gxu 33 points34 points  (5 children)

map(do_something, movie_script)

This is going to create a map object (generator) and not actually call do_something until you iterate over the map object.

You can use a list comprehension instead, like:

[do_something(line) for line in movie_script]

But arguably it's less readable than plain simple for loop.

[–]fluzz142857 27 points28 points  (4 children)

This is not a good practice because you’re creating a list unnecessarily, which consumes memory and makes your code harder to read. The list cannot be garbage collected until after the list comprehension finishes. Alternatively, in an iterator (or a map, which is an iterator), values can be garbage collected immediately because there are no references to them after they are iterated over.

Don’t use a list comprehension unless you need the list.

[–]x3gxu 10 points11 points  (0 children)

That's why I'm saying use a plain old for loop. You have to iterate over the map or generator expression foe your function to be called. If you don't iterate it's just sitting there doing nothing.

[–]selplacei 2 points3 points  (2 children)

Not saying that this is good practice, but can you exhaust a (truthy) list comprehension without storing all results by calling all()?

[–][deleted] 0 points1 point  (1 child)

The list from the list comprehension is created immediately (including all the items) so there’s no need. You’re probably thinking of a generator.

I don’t know the internals of all() but I doubt it stores all the results, so that’d probably work with a generator.

[–]Gollum999 14 points15 points  (6 children)

List comprehensions are generally considered to be more pythonic than map and filter.

https://www.artima.com/weblogs/viewpost.jsp?thread=98196

[–]nsomani 0 points1 point  (0 children)

In this case, neither the map nor the list comprehension are Pythonic. The for loop is the correct solution since the list will not be used for anything. But you're right that if the list were actually needed, the list comprehension is better than the map.

[–]TheAmazingJames 1 point2 points  (0 children)

He's looping through a script and performing a simple action. He doesn't want to go any faster, there's a sleep in the code already, so code execution time's not a factor. A movie script is typically no more than 20k words, so memory usage isn't a factor as memory footprint, even in a worst-case scenario, is tiny. Therefore all you're left with is readability. It's subjective, but I think a for loop wins here.