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

    The other answers explain it well but I just wanted to add a bit more about which conditional expression to use in a list comprehension. As the docs state, list comprehensions are used to create a list using …

  2. python - if else in a list comprehension - Stack Overflow

    Feb 2, 2013 · The point was to show the difference between the if in the ternary A if C else B and the conditional if in i for i in items if p(i). Every comprehension can be written as statements if you name …

  3. How to use a Python list comprehension with a conditional expression ...

    Jul 15, 2015 · How to use a Python list comprehension with a conditional expression [duplicate] Asked 10 years, 6 months ago Modified 10 years, 6 months ago Viewed 2k times

  4. python - using conditional with list comprehension - Stack Overflow

    Aug 4, 2020 · I'm learning the ropes of python programming and I'm struggling with what might seem simple but confusing to me. I've got the list my_list and i'd like to subtract 3, only from numbers …

  5. python - List comprehension with if statement - Stack Overflow

    Mar 18, 2013 · You can't put an else in a list comprehension, not where you put one at least. Don't confuse a list comprehension (filtering), with a conditional expression (which must have a value, …

  6. python - List comprehension with condition - Stack Overflow

    Jun 27, 2014 · See if/else in a list comprehension if you are trying to make a list comprehension that uses different logic based on a condition.

  7. python - Conditional list comprehension - Stack Overflow

    Apr 4, 2017 · I am trying to gain a better understanding of how list comprehensions work. I have the following function that returns true or false if a number is prime (which I found somewhere on the …

  8. Multiple IF conditions in a python list comprehension

    Apr 23, 2012 · Ok so that's a list of conditions, but how do I use them in the list comprehension? And also, to put i in the list (when i is not divisible by any number), the code should be such that it knows …

  9. python - How to conditionally select elements in a list comprehension ...

    Jan 14, 2020 · 1 To answer the question in your title, you can conditionally add elements during a list comprehension using the syntax [x for y in z if y == a], where y == a is any condition you need - if the …

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

    Think about how you would write the explicit loop using only if and else, if elif didn't exist. Then translate that into what you already know about using if and else (i.e., the ternary operator) in a list …