About 156,000 results
Open links in new tab
  1. python - if/else in a list comprehension - Stack Overflow

    List comprehension where an if statement follows a for statement (case (2) above) is similar to the math notation to define a subset from a given set, so [x for x in iterable if m<x<n] is similar to …

  2. python - Create a dictionary with comprehension - Stack Overflow

    Create a dictionary with list comprehension in Python I like the Python list comprehension syntax. Can it be used to create dictionaries too? For example, by iterating over pairs of keys and …

  3. python - List comprehension list of lists - Stack Overflow

    Feb 2, 2014 · I have a list of lists, and would like to use list comprehension to apply a function to each element in the list of lists, but when I do this, I end up with one long list rather than my list …

  4. How can I use list comprehensions to process a nested list?

    Here is how you would do this with a nested list comprehension: [[float(y) for y in x] for x in l] This would give you a list of lists, similar to what you started with except with floats instead of …

  5. python - How do I make a flat list out of a list of lists? - Stack …

    If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list …

  6. python - Are list-comprehensions and functional functions faster …

    Mar 1, 2014 · List comprehensions aren't magic that is inherently faster than a good old loop. As for functional list processing functions: While these are written in C and probably outperform …

  7. python - Lambda function in list comprehensions - Stack Overflow

    Why is the output of the following two list comprehensions different, even though f and the lambda function are the same? f = lambda x: x*x [f(x) for x in range(10)] and [lambda x: x*x for x in r...

  8. "If...or..." statement inside list comprehension? - Stack Overflow

    Dec 26, 2014 · python python-3.x list-comprehension edited Dec 26, 2014 at 2:22 asked Dec 26, 2014 at 2:17 GreenRaccoon23

  9. Add an element in each dictionary of a list (list comprehension)

    Dec 28, 2012 · You are using a list comprehension incorrectly, the call to returns a value and thus your newly created list will be full of values instead of your expected values. You need only to …

  10. python - elif in list comprehension conditionals - Stack Overflow

    How can we represent the elif logic in a list comprehension? Up until now, I have only used if and else in list comprehension, as in if/else in a list comprehension.