Linked Questions
175 questions linked to/from List comprehension vs map
4
votes
2
answers
8k
views
Map vs list comprehension in Python [duplicate]
When should you use map/filter instead of a list comprehension or generator expression?
12
votes
1
answer
2k
views
Python Zen - (only) one way to do it [duplicate]
This question might be sound subjective, but as "the Zen" says, there is (nearly always) one way to preferred, it shouldn't be subjective at the end.
What way is the better one?
[i.something() for i ...
1
vote
1
answer
13k
views
Is it better complexity wise to use the map() function in python or a comprehension? [duplicate]
As the question states.
map(f, iterable) can be written as [f(x) for x in iterable].
Which is better to use? And why?
As an example,
I would like to convert a list of strings to int.
ip = (...
2
votes
1
answer
4k
views
python: map() or list comprehension? [duplicate]
pythonistas: which is faster, where a is something like [ str(x) for x in list(range(100)) ]?
ints = map(int, a)
Or
ints = [ int(x) for x in a ]
Assuming a will be a relatively large list of ...
0
votes
4
answers
1k
views
Python map vs list comprehension [duplicate]
When comparing these, which do you think is more intuitive / easier to read?
>>> [ord(i) for i in 'some string']
[115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]
>>> map(ord,'...
tMC's user avatar
- 19.5k
2
votes
1
answer
496
views
why not always use map if its faster than the rest (list comprehension, loop (various variants))? [duplicate]
I came across a few articles and StackOverflow discussions where it is usually recommended to use list comprehension while dealing with lists.
I usually come across a common loop structure where I ...
1
vote
1
answer
401
views
Is this simplified version of map/lambda in python? [duplicate]
For map/lambda expression with array a = [1, 2, 3, 4]:
f = map(lambda x : x + 32, a)
Seems I can simplify write as:
f = [x + 32 for x in a]
I am wondering whether there is any difference.
2
votes
1
answer
294
views
Is list comprehension implemented via map and lambda function? [duplicate]
According to the list comprehension doc and this question: squares = [x**2 for x in range(10)] is equivalent to squares = list(map(lambda x: x**2, range(10)))
But does Python actually implement list ...
3
votes
1
answer
181
views
When and why to map a lambda function to a list [duplicate]
I am working through a preparatory course for a Data Science bootcamp and it goes over the lambda keyword and map and filter functions fairly early on in the course. It gives you syntax and how to use ...
1
vote
2
answers
198
views
What does `var = [ ... ]` do in Python? [duplicate]
In my Python program, I was told that this code snippet:
listOfNumbers = input() # (This inputs a space separated list of integers)
listOfNumbers = listOfNumbers.split()
for x in range(0, len(...
0
votes
3
answers
105
views
How can I create a product of a 2-dimensional list with a 1-dimensional list (needs to be one-liner)? [duplicate]
I have two lists that I'm trying to combine:
lst1 = [['arg1', 'arg2']] # this can have more lists, but for simplicity, leaving it as this
lst2 = ['arg3', 'arg4']
I want the product of the two lists ...
2
votes
0
answers
128
views
Python 3.5 Why is map() slower than list comprehension? [duplicate]
(i5 ivybridge cpu windows 10) Map seems to come out slower every time, for what is a very long list, where it should do well
import time
l = [2049 for i in range(20000000)]
def f(e):
return (e * ...
0
votes
0
answers
64
views
Python map and filter, am I missing the point, clarity, speed? [duplicate]
I have an application where I have a list of objects, and I need to create a sub list of those objects for which some property is minimum. I naturally started writing for thing in list: type code, and ...
659
votes
19
answers
394k
views
Python progression path - From apprentice to guru
I've been learning, working, and playing with Python for a year and a half now. As a biologist slowly making the turn to bio-informatics, this language has been at the very core of all the major ...
Community wiki
814
votes
6
answers
476k
views
What is the purpose of the single underscore "_" variable in Python?
What is the meaning of _ after for in this code?
if tbh.bag:
n = 0
for _ in tbh.bag.atom_set():
n += 1