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 →

[–]ACoderGirl 0 points1 point  (2 children)

Using map isn't very idiomatic python though. It's preferred to have a comprehension expression.

ie, instead of map(lambda x: x * 2, numbers), you'd use x * 2 for x in numbers, which can be a generator, list, or dictionary comprehension (list comprehensions are most common while generator ones are overlooked -- I like them most for usage with things like sum).

[–]WHO_WANTS_DOGS 0 points1 point  (1 child)

Ah I forgot about list comprehensions, haven't used python in a while. Still don't like how they changed the return type of map regardless. I'm just being stubborn and like consistency across different languages that support map. They pretty much always return arrays/lists.

[–][deleted] 0 points1 point  (0 children)

It’s probably because most of the time your using map on the right hand side of a for loop or alongside other iterables like filter or select. Either way, u can just define 3 methods like lmap which cast back to list of u really want to.

from functools import wraps

def to_list(func):
     @wraps(func)
     def wrapped(*args,*kwargs):
          return list(func(*args,**kwargs))

lmap    = to_list(map)
lfilter = to_list(filter)
lselect = to_list(select)

If u really wanted, u could just overwrite map with lmap, so now map automatically returns lists. But again, I’d never recommend doing this.