About 158,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 - What does "list comprehension" and similar mean? How …

    I've searched and it seems this is called a list comprehension and similarly there seem to be set/dict comprehensions and generator expressions. But how does it work?

  6. 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 …

  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. 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.

  9. "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

  10. Is it possible to add a where clause with list comprehension?

    Jul 23, 2012 · Consider the following list comprehension [ (x,f(x)) for x in iterable if f(x) ] This filters the iterable based a condition f and returns the pairs of x,f(x). The problem with this approach …