python if else comprehension with two lists

python list comprehension if else pass
python list comprehension if elif else
python list comprehension if without else
nested if else in list comprehension python
list comprehension python 3 if
python list comprehension multiple variables
python list comprehension two lists
nested list comprehension python

I have this code:

a = []
b = []
for row in range(10):
    if x[row] is True:
        a += list_features[row]
    else:
        b += list_features[row]

Where list_features[row] is a 1D list. I would like to do this in list comprehension I know something like the below code would work:

a+= [list_features[row] for row in (10) if x[row] is True]
b+= [list_features[row] for row in (10) if x[row] is False]

How would one do this with just one comprehension in one line?

since you have 2 targets, list comprehension isn't adapted (and using 2 comprehensions means testing x[row] twice, once to build a and once to build b)

Something that would be slightly more concise would be a ternary expression to choose the target (you cannot use += directly on an expression but you can use extend which does the same thing):

(a if x[row] else b).extend(list_features[row])

that said, I would not use row to index both lists (cumbersome, unpythonic and underperformant when doing sequential access), but zip:

for i,lf in zip(x,list_features):
   (a if i else b).extend(lf)

the only "risk" is that one of the lists is shorter than the other, in which case, you don't get any error, and the result is incorrect (a proper fillvalue given to itertools.zip_longest can be an alternative in that case)

Python list comprehension with multiple lists and an if else conditon , First of all, forgo the list comprehension; feed the values directly into sum() with a generator expression: sum(1 for a in A if a in arr). If A is a set,  You can also use an if-else in a list comprehension in Python. Since in a comprehension, the first thing we specify is the value to put in a list, this is where we put our if-else. >>> ["Even" if i%2==0 else "Odd" for i in range(8)] [‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’]

Since the question is to do it in one line:

a, b = [list_features[row] for row in range(10) if x[row]], [list_features[row] for row in range(10) if not x[row]]

This doesn't assume that x or list_features are sequences, they could be dicts.

But there's really no point in putting everything in one line. Writing clear code is more important.

Python List Comprehension (Syntax & Examples), Remember, this is a nested conditional, not an AND operation of two conditions. b. if..else in List Comprehension in Python. You can also use an if-else in a list  Yes, else can be used in Python inside a list comprehension with a Conditional Expression ("ternary operator"): Here, the parentheses "()" are just to emphasize the conditional expression, they are not necessarily required (Operator precedence).

Your list comprehensions are almost fine, so you don´t need the first 2 lines, but only list comprehensions, so instead of:

a = []
b = []
for row in range(10):
    if x[row] is True:
        a += list_features[row]
    else:
        b += list_features[row]

use

a = [list_features[row] for row in range(10) if x[row] is True]
b = [list_features[row] for row in range(10) if x[row] is False]

Note the range() function.

Python List Comprehensions in 5-minutes, What else can you do with Python list comprehensions? Now you've got a little bit of a list comprehension? cashier_3 = [item for item in cart if item % 2 == 0] And, for list comprehensions with if conditions only, [f(x) for x in sequence if condition] Note that this actually uses a different language construct, a conditional expression, which itself is not part of the comprehension syntax, while the if after the for…in is part of list comprehensions and used to filter elements from the source iterable.

Use zip() and get rid of row counter, since you don't need it

a = [feature for feature, enabled in zip(list_features, x) if enabled]
b = [feature for feature, enabled in zip(list_features, x) if not enabled]

5. Data Structures, List comprehensions provide a concise way to create lists. For example, this listcomp combines the elements of two lists if they are not equal: >>> [(x, y) for x Note how the order of the for and if statements is the same in both these snippets. List comprehensions provide a concise way to create lists. It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The expressions can be anything, meaning you can put in all kinds of objects in lists. The result will be a new list resulting from evaluating

When you use list comprehension, you're constructing a list by following a pattern, you're not adding an element to an existing list. Which also means you don't have to declare it first.

The correct syntax is a = [[list_features[row] for row in range(10) if x[row] is True]]

Also you don't have to write if x[row] is true explicitly, since I suppose x is an array of boolean.

a = [[list_features[row] for row in range(10) if x[row]]]

Nested List Comprehensions in Python, Hence if sublist = [1, 2, 3], 'for val in sublist' –> gives 1, 2, 3 as output one by one. For every such val, we get the output as val and we append it to the list. Example​  List comprehensions are equivalent to for-loops. Therefore, [x + y for x in l2 for y in l1 ] would become: new_list = [] for x in l2: for y in l1: new_list.append(x + y) Whereas zip returns tuples containing one element from each list.

Python Language, even_numbers = [] for x in range(10): if x % 2 == 0: even_numbers.append(x) print(even_numbers) # Out: [0, 2, 4, 6, 8]. Also, a conditional list comprehension of  python list list-comprehension if-statement. Share a link to this question. improve this question. edited Dec 26 '11 at 15:32. 46 silver badges. 89 bronze badges. asked Dec 10 '10 at 6:45. 152 silver badges. 175 bronze badges. marked as duplicate by Dan D. python. Users with the python badge can single-handedly close python questions as

Python List Comprehension (With Examples), In this article, we will learn about Python list comprehensions, and how to use it. 2. obj = ["Even" if i%2==0 else "Odd" for i. in range(10)]. print(obj). List comprehensions are one of the really nice and powerful features of Python. It is actually a smart way to introduce new users to functional programming concepts (after all a list comprehension is just a combination of map and filter) and compact statements. However, one thing that always troubled me when using list comprehensions is their

List Comprehension Python - Python List Comprehension, The syntax of Python lists consists of two square brackets inside of which Example3: Using if else statement with Python List comprehension Python Language Iterate two or more list simultaneously within list comprehension Example For iterating more than two lists simultaneously within list comprehension , one may use zip() as:

Comments
  • Why do you need list comprehension here?
  • What is x here?
  • x is a boolean value taken from another list of boolean values. It doesn't matter really, code is pretty simple I just wanted to know if its possible.
  • you can lose the outer [] as without it it creates a tuple which unpacks very well into a,b. +1 for "they could be dicts" which justifies the usage of the subscript notation instead of zip