About 105,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 {x ∈ iterable | …

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

    54 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 values:

  3. python - How do I make a flat list out of a list of lists? - Stack Overflow

    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 …

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

  5. python - List comprehension vs. lambda + filter - Stack Overflow

    I find the list comprehension much clearer than filter + lambda, but use whichever you find easier. There are two things that may slow down your use of filter. The first is the function call overhead: as soon …

  6. python - List comprehension: Returning two (or more) items for each ...

    When reasonably possible, you should prefer using itertools.product() over using two for clauses in a list comprehension. Some style guides, such as the Google Python Style Guide forbid using multiple for …

  7. python - NumPy List Comprehension Syntax - Stack Overflow

    I'd like to be able to use list comprehension syntax to work with NumPy arrays easily. For instance, I would like something like the below obviously wrong code to just reproduce the same array. &...

  8. python - Double Iteration in List Comprehension - Stack Overflow

    In Python you can have multiple iterators in a list comprehension, like [(x,y) for x in a for y in b] for some suitable sequences a and b. I'm aware of the nested loop semantics of Python's list

  9. python - How can I use list comprehensions to process a nested list ...

    How can I use list comprehensions to process a nested list? Asked 12 years, 4 months ago Modified 1 month ago Viewed 266k times

  10. python - Nested For Loops Using List Comprehension - Stack Overflow

    Sep 3, 2010 · python for-loop list-comprehension asked Sep 3, 2010 at 4:57 John Howard 64.9k 23 53 67