I have a list xs containing a mixture of strings and None values. How can I use a list comprehension to call a function on each string, but convert the None values to '' (rather than passing them to the function)?
I tried:
[f(x) for x in xs if x is not None else '']
but it gives a SyntaxError. What is the correct syntax?
See List comprehension with condition if you are trying to make a list comprehension that omits values based on a condition.
If you need to consider more than two conditional outcomes, beware that Python's conditional expressions do not support elif. Instead, it is necessary to nest if/else conditionals. See `elif` in list comprehension conditionals for details.
You can totally do that. It's just an ordering issue:
[f(x) if x is not None else '' for x in xs]
In general,
[f(x) if condition else g(x) for x in sequence]
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.
Conditional expressions can be used in all kinds of situations where you want to choose between two expression values based on some condition. This does the same as the ternary operator ?: that exists in other languages. For example:
value = 123
print(value, 'is', 'even' if value % 2 == 0 else 'odd')
Let's use this question to review some concepts. I think it's good to first see the fundamentals so you can extrapolate to different cases.
Other answers provide the specific answer to your question. I'll first give some general context and then I'll answer the question.
Fundamentals
if/else statements in list comprehensions involve two things:
List comprehensions
Conditional expressions (Ternary operators)
1. List comprehensions
They provide a concise way to create lists.
Its structure consists of: "brackets containing an expression followed by a for clause, then zero or more for or if clauses".
Case 1
Here we have no condition. Each item from the iterable is added to new_list.
new_list = [expression for item in iterable]
new_list = [x for x in range(1, 10)]
> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Case 2
Here we have one condition.
Example 1
Condition: only even numbers will be added to new_list.
new_list = [expression for item in iterable if condition == True]
new_list = [x for x in range(1, 10) if x % 2 == 0]
> [2, 4, 6, 8]
Example 2
Condition: only even numbers that are multiple of 3 will be added to new_list.
new_list = [expression for item in iterable if condition == True]
new_list = [x for x in range(1, 10) if x % 2 == 0 if x % 3 == 0]
> [6]
But howcome we have one condition if we use two if in new_list?
The prior expression could be written as:
new_list = [x for x in range(1, 10) if x % 2 and x % 3 == 0]
> [6]
We only use one if statement.
This is like doing:
new_list = []
for x in range(1, 10):
if x % 2 == 0 and x % 3 == 0:
new_list.append(x)
> [6]
Example 3
Just for the sake of argument, you can also use or.
Condition: even numbers or numbers multiple of 3 will be added to new_list.
new_list = [x for x in range(1, 10) if x % 2 == 0 or x % 3 == 0]
> [2, 3, 4, 6, 8, 9]
Case 3
More than one condition:
Here we need the help of conditional expressions (Ternary operators).
2.Conditional Expressions
What are conditional expressions? What the name says: a Python expression that has some condition.
<Exp1> if condition else <Exp2>
First the condition is evaluated. If condition is True, then <Exp1> is evaluated and returned. If condition is False, then <Exp2> is evaluated and returned.
A conditional expression with more than one condition:
<Exp1> if condition else <Exp2> if condition else <Exp3>...
An example from Real Python:
age = 12
s = 'minor' if age < 21 else 'adult'
> minor
The value of s is conditioned to age value.
3.List Comprehensions with Conditionals
We put list comprehensions and conditionals together like this.
new_list = [<Conditional Expression> for <item> in <iterable>]
new_list = [<Exp1> if condition else <Exp2> if condition else <Exp3> for <item> in <iterable>]
Condition: even numbers will be added as 'even', the number three will be added as 'number three' and the rest will be added as 'odd'.
new_list = ['even' if x % 2 == 0 else 'number three' if x == 3 else 'odd'
for x in range(1, 10)]
> ['odd', 'even', 'number three', 'even', 'odd', 'even', 'odd', 'even', 'odd']
The answer to the question
[f(x) for x in xs if x is not None else '']
Here we have a problem with the structure of the list: for x in xs should be at the end of the expression.
Correct way:
[f(x) if x is not None else '' for x in xs]
Further reading:
Does Python have a ternary conditional operator?
The specific problem has already been solved in previous answers, so I will address the general idea of using conditionals inside list comprehensions.
Here is an example that shows how conditionals can be written inside a list comprehension:
X = [1.5, 2.3, 4.4, 5.4, 'n', 1.5, 5.1, 'a'] # Original list
# Extract non-strings from X to new list
X_non_str = [el for el in X if not isinstance(el, str)] # When using only 'if', put 'for' in the beginning
# Change all strings in X to 'b', preserve everything else as is
X_str_changed = ['b' if isinstance(el, str) else el for el in X] # When using 'if' and 'else', put 'for' in the end
Note that in the first list comprehension for X_non_str, the order is:
expression for item in iterable if condition
and in the last list comprehension for X_str_changed, the order is:
expression1 if condition else expression2 for item in iterable
I always find it hard to remember that expression1 has to be before if and expression2 has to be after else. My head wants both to be either before or after.
I guess it is designed like that because it resembles normal language, e.g. "I want to stay inside if it rains, else I want to go outside"
In plain English the two types of list comprehensions mentioned above could be stated as:
With only if:
extract_apple for apple in apple_box if apple_is_ripe
and with if/else
mark_apple if apple_is_ripe else leave_it_unmarked for apple in apple_box
One way:
def change(x):
if x is None:
return f(x)
else:
return ''
result = [change(x) for x in xs]
Although then you have:
result = map(change, xs)
Or you can use a lambda inline.
Here is another illustrative example:
>>> print(", ".join(["ha" if i else "Ha" for i in range(3)]) + "!")
Ha, ha, ha!
It exploits the fact that if i evaluates to False for 0 and to True for all other values generated by the function range(). Therefore the list comprehension evaluates as follows:
>>> ["ha" if i else "Ha" for i in range(3)]
['Ha', 'ha', 'ha']
[f(x) if x != None else '' for x in xs]
Syntax for list comprehension:
[item if condition else item for item in items]
[f(item) if condition else value for item in items]
[item if condition for item in items]
[value if condition else value1 if condition1 else value2]
The other solutions are great for a single if / else construct. However, ternary statements within list comprehensions are arguably difficult to read.
Using a function aids readability, but such a solution is difficult to extend or adapt in a workflow where the mapping is an input. A dictionary can alleviate these concerns:
xs = [None, 'This', 'is', 'a', 'filler', 'test', 'string', None]
d = {None: '', 'filler': 'manipulated'}
res = [d.get(x, x) for x in xs]
print(res)
['', 'This', 'is', 'a', 'manipulated', 'test', 'string', '']
It has to do with how the list comprehension is performed.
Keep in mind the following:
[ expression for item in list if conditional ]
Is equivalent to:
for item in list:
if conditional:
expression
Where the expression is in a slightly different format (think switching the subject and verb order in a sentence).
Therefore, your code [x+1 for x in l if x >= 45] does this:
for x in l:
if x >= 45:
x+1
However, this code [x+1 if x >= 45 else x+5 for x in l] does this (after rearranging the expression):
for x in l:
if x>=45: x+1
else: x+5
Make a list from items in an iterable
It seems best to first generalize all the possible forms rather than giving specific answers to questions. Otherwise, the reader won't know how the answer was determined. Here are a few generalized forms I thought up before I got a headache trying to decide if a final else' clause could be used in the last form.
[expression1(item) for item in iterable]
[expression1(item) if conditional1 for item in iterable]
[expression1(item) if conditional1 else expression2(item) for item in iterable]
[expression1(item) if conditional1 else expression2(item) for item in iterable if conditional2]
The value of item doesn't need to be used in any of the conditional clauses. A conditional3 can be used as a switch to either add or not add a value to the output list.
For example, to create a new list that eliminates empty strings or whitespace strings from the original list of strings:
newlist = [s for s in firstlist if s.strip()]
There isn't any need for ternary if/then/else. In my opinion your question calls for this answer:
row = [unicode((x or '').strip()) for x in row]
You can combine conditional logic in a comprehension:
ps = PorterStemmer()
stop_words_english = stopwords.words('english')
best = sorted(word_scores.items(), key=lambda x: x[1], reverse=True)[:10000]
bestwords = set([w for w, s in best])
def best_word_feats(words):
return dict([(word, True) for word in words if word in bestwords])
# with stemmer
def best_word_feats_stem(words):
return dict([(ps.stem(word), True) for word in words if word in bestwords])
# with stemmer and not stopwords
def best_word_feats_stem_stop(words):
return dict([(ps.stem(word), True) for word in words if word in bestwords and word not in stop_words_english])
# coding=utf-8
def my_function_get_list():
my_list = [0, 1, 2, 3, 4, 5]
# You may use map() to convert each item in the list to a string,
# and then join them to print my_list
print("Affichage de my_list [{0}]".format(', '.join(map(str, my_list))))
return my_list
my_result_list = [
(
number_in_my_list + 4, # Condition is False : append number_in_my_list + 4 in my_result_list
number_in_my_list * 2 # Condition is True : append number_in_my_list * 2 in my_result_list
)
[number_in_my_list % 2 == 0] # [Condition] If the number in my list is even
for number_in_my_list in my_function_get_list() # For each number in my list
]
print("Affichage de my_result_list [{0}]".format(', '.join(map(str, my_result_list))))
(venv) $ python list_comp.py
Affichage de my_list [0, 1, 2, 3, 4, 5]
Affichage de my_result_list [0, 5, 4, 7, 8, 9]
So, for you:
row = [('', unicode(x.strip()))[x is not None] for x in row]
Related
How would I check if the first digits in each element in a list are the same?
for i in range(0,len(lst)-1):
if lst[i] == lst[i+1]:
return True
I know that this checks for if the number before is equal to the next number in the list, but I just want to focus on the first digit.
You can use math.log10 and floor division to calculate the first digit. Then use all with a generator expression and zip to test adjacent elements sequentially:
from math import log10
def get_first(x):
return x // 10**int(log10(x))
L = [12341, 1765, 1342534, 176845, 1]
res = all(get_first(i) == get_first(j) for i, j in zip(L, L[1:])) # True
For an explanation of how this construct works, see this related answer. You can apply the same logic via a regular for loop:
def check_first(L):
for i, j in zip(L, L[1:]):
if get_first(i) != get_first(j):
return False
return True
res = check_first(L) # True
Use all() as a generator for the first character(s) of your numbers:
>>> l = [1, 10, 123]
>>> all(str(x)[0] == str(l[0])[0] for x in l)
True
The list comprehension
>>> [str(x)[0] for x in l]
creates a list
['1', '1', '1']
which sounds as if this should be enough. But all processes boolean values, and the boolean value of a string is always True, except when the string is empty. That means that it would also consider ['1','2','3'] to be True. You need to add a comparison against a constant value -- I picked the first item from the original list:
>>> [str(x)[0] == str(l[0])[0] for x in l]
[True, True, True]
whereas a list such as [1,20,333] would show
['1', '2', '3']
and
[True, False, False]
You can adjust it for a larger numbers of digits as well:
>>> all(str(x)[:2] == str(l[0])[:2] for x in l)
False
>>> l = [12,123,1234]
>>> all(str(x)[:2] == str(l[0])[:2] for x in l)
True
You could do something like this:
lst = [12, 13, 14]
def all_equals(l):
return len(set(e[0] for e in map(str, l))) == 1
print all_equals(lst)
Output
True
Explanation
The function map(str, l) converts all elements in the list to string then with (e[0] for e in map(str, l)) get the first digit of all the elements using a generator expression. Finally feed the generator into the set function this will remove all duplicates, finally you have to check if the length of the set is 1, meaning that all elements were duplicates.
For a boolean predicate on a list like this, you want a solution that returns False as soon as a conflict is found -- solutions that convert the entire list just to find the first and second item didn't match aren't good algorithms. Here's one approach:
def all_same_first(a):
return not a or all(map(lambda b, c=str(a[0])[0]: str(b)[0] == c, a[1:]))
Although at first glance this might appear to violate what I said above, the map function is lazy and so only hands the all function what it needs as it needs it, so as soon as some element doesn't match the first (initial-digit-wise) the boolean result is returned and the rest of the list isn't processed.
Going back to your original code:
this checks for if the number before is equal to the next
number in the list
for i in range(0,len(lst)-1):
if lst[i] == lst[i+1]:
return True
This doesn't work, as you claim. To work properly, it would need to do:
for i in range(0, len(lst) - 1):
if lst[i] != lst[i + 1]:
return False
return True
Do you see the difference?
Let's say I have list = [1,2,3,'hello','bye',10,11,12,12.2,12.3] I want to return the elements that are in an odd index (zero based) OR they are integers. The idea is to make it the code as simple as possible.
Thanks!
You can use list comprehension:
[x for i,x in enumerate(lst) if i%2 == 1 or isinstance(x,int)]
Here we make use of enumerate(..) to generate tuples (i,x) with the index (zero-based) and the element. The if in the list filters: only elements where i%2 == 1 (odd index) or where x is an instance of int are allowed. For these elements, we add x to the result.
The result is:
>>> [x for i,x in enumerate(lst) if i%2 == 1 or isinstance(x,int)]
[1, 2, 3, 'hello', 10, 11, 12, 12.3]
Please do not use list as variable name: it is the name of a class, so by using list, you can no longer use list(..). This answer works with lst.
This question already has answers here:
How can I partition (split up, divide) a list based on a condition?
(41 answers)
Closed 6 years ago.
is it possible to write list comprehensions for the following python code:
for str in range(0,len(mixed_content)):
if (mixed_content[str].isdigit()):
num_list.append(mixed_content[str])
else:
string_list.append(mixed_content[str])
can we use else block in list comprehensions ? I tried to write list comprehensions for above code :
num_list , string_list = [ mixed_content[str] for str in range(0,len(mixed_content)) if(mixed_content[str].isdigit()) else ]
It's not possible as-is, but if you're looking for one-liners you can do that with a ternary expression inside your loop (saves a test and is compact):
num_list=[]
string_list=[]
for s in ["45","hello","56","foo"]:
(num_list if s.isdigit() else string_list).append(s)
print(num_list,string_list)
result:
['45', '56'] ['hello', 'foo']
Notes:
despite the parenthesized syntax and the context of the question, (num_list if s.isdigit() else string_list) is not a generator, a ternary expression (protected between parentheses to counter .append precedence) which returns num_list if s is a sequence of (positive) digits and string_list if s otherwise.
this code won't work with negative numbers because isdigits will return false. You'll have to code a special function for that (classical problem here on SO)
You can only construct one list at a time with list comprehension. You'll want something like:
nums = [foo for foo in mixed_list if foo.isdigit()]
strings = [foo for foo in mixed_list if not foo.isdigit()]
Here is an example of using x if b else y in a list comprehension.
mixed_content = "y16m10"
num_list, string_list = zip(
*[(ch, None) if ch.isdigit() else (None, ch) for ch in mixed_content])
num_list = filter(None, num_list)
string_list = filter(None, string_list)
print num_list, string_list
Let's initialize variables:
>>> mixed_content='ab42c1'; num_list=[]; string_list=[]
Because the OP asked about using "if-else in python list comprehensions," let's show that that can be done:
>>> [num_list.append(c) if c.isdigit() else string_list.append(c) for c in mixed_content]
[None, None, None, None, None, None]
Let's verify that we have the lists that you want:
>>> num_list, string_list
(['4', '2', '1'], ['a', 'b', 'c'])
You can accomplish what you want with two list comprehensions:
num_list = [num for num in mixed_content if num.isdigit()]
string_list = [string for string in mixed_content if not string.isdigit()]
The else clause is not supported in list comprehensions:
>>> [c for c in range(5) if c == 1 else 0]
SyntaxError: invalid syntax
A super messy and unpractical way of doing is:
mixed_content = ['a','b','c',"4"]
string_list = []
print [y for y in [x if mixed_content[x].isdigit() else string_list.append(mixed_content[x]) for x in range(0,len(mixed_content))] if y != None]
print string_list
Returns:
[3]
['a', 'b', 'c']
Basically list comprehension for your condition and then filter that list comprehension to only accept non None
I want to use the length of my list for which I'm generating a list comprehension as a condition along with those in the list comprehension that is being passed to the any() function itself
I could do this in two lines -
li = [1,2,3,4]
lcond = [x for x in li if x > 3]
any(lcond) and len(lcond) >2
But I'd like to do it in one
You don't really need a list comprehension here.
if len(filter(lambda x: x > 3, li)) > 2:
In Python 3, you need to explicitly consume the generator returned by filter:
if len(list(filter(lambda x: x > 3, li))) > 2:
Actually, why am I even using filter here?
if len([x for x in li if x > 3]) > 2:
any is unnecessary, because a non-zero length of the list implies that any(...) would return true.
If your list is a very large one I recommend you don't
go through the whole list just for knowing if there are at least two items that satisfies the conditions.
I suggest to use islice from itertools applied over a generator like that:
test_list = [1,2,3,4]
# Returns False
len(list(itertools.islice(filter(lambda x: x>3, test_list), 2))) == 2
test_list = [1,2,3,4,5,6,7,8]
# Returns True
len(list(itertools.islice(filter(lambda x: x>3, test_list), 2))) == 2
It is more complex but if the list is huge it's better don't go through the whole list.
I have a list xs containing a mixture of strings and None values. How can I use a list comprehension to call a function on each string, but convert the None values to '' (rather than passing them to the function)?
I tried:
[f(x) for x in xs if x is not None else '']
but it gives a SyntaxError. What is the correct syntax?
See List comprehension with condition if you are trying to make a list comprehension that omits values based on a condition.
If you need to consider more than two conditional outcomes, beware that Python's conditional expressions do not support elif. Instead, it is necessary to nest if/else conditionals. See `elif` in list comprehension conditionals for details.
You can totally do that. It's just an ordering issue:
[f(x) if x is not None else '' for x in xs]
In general,
[f(x) if condition else g(x) for x in sequence]
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.
Conditional expressions can be used in all kinds of situations where you want to choose between two expression values based on some condition. This does the same as the ternary operator ?: that exists in other languages. For example:
value = 123
print(value, 'is', 'even' if value % 2 == 0 else 'odd')
Let's use this question to review some concepts. I think it's good to first see the fundamentals so you can extrapolate to different cases.
Other answers provide the specific answer to your question. I'll first give some general context and then I'll answer the question.
Fundamentals
if/else statements in list comprehensions involve two things:
List comprehensions
Conditional expressions (Ternary operators)
1. List comprehensions
They provide a concise way to create lists.
Its structure consists of: "brackets containing an expression followed by a for clause, then zero or more for or if clauses".
Case 1
Here we have no condition. Each item from the iterable is added to new_list.
new_list = [expression for item in iterable]
new_list = [x for x in range(1, 10)]
> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Case 2
Here we have one condition.
Example 1
Condition: only even numbers will be added to new_list.
new_list = [expression for item in iterable if condition == True]
new_list = [x for x in range(1, 10) if x % 2 == 0]
> [2, 4, 6, 8]
Example 2
Condition: only even numbers that are multiple of 3 will be added to new_list.
new_list = [expression for item in iterable if condition == True]
new_list = [x for x in range(1, 10) if x % 2 == 0 if x % 3 == 0]
> [6]
But howcome we have one condition if we use two if in new_list?
The prior expression could be written as:
new_list = [x for x in range(1, 10) if x % 2 and x % 3 == 0]
> [6]
We only use one if statement.
This is like doing:
new_list = []
for x in range(1, 10):
if x % 2 == 0 and x % 3 == 0:
new_list.append(x)
> [6]
Example 3
Just for the sake of argument, you can also use or.
Condition: even numbers or numbers multiple of 3 will be added to new_list.
new_list = [x for x in range(1, 10) if x % 2 == 0 or x % 3 == 0]
> [2, 3, 4, 6, 8, 9]
Case 3
More than one condition:
Here we need the help of conditional expressions (Ternary operators).
2.Conditional Expressions
What are conditional expressions? What the name says: a Python expression that has some condition.
<Exp1> if condition else <Exp2>
First the condition is evaluated. If condition is True, then <Exp1> is evaluated and returned. If condition is False, then <Exp2> is evaluated and returned.
A conditional expression with more than one condition:
<Exp1> if condition else <Exp2> if condition else <Exp3>...
An example from Real Python:
age = 12
s = 'minor' if age < 21 else 'adult'
> minor
The value of s is conditioned to age value.
3.List Comprehensions with Conditionals
We put list comprehensions and conditionals together like this.
new_list = [<Conditional Expression> for <item> in <iterable>]
new_list = [<Exp1> if condition else <Exp2> if condition else <Exp3> for <item> in <iterable>]
Condition: even numbers will be added as 'even', the number three will be added as 'number three' and the rest will be added as 'odd'.
new_list = ['even' if x % 2 == 0 else 'number three' if x == 3 else 'odd'
for x in range(1, 10)]
> ['odd', 'even', 'number three', 'even', 'odd', 'even', 'odd', 'even', 'odd']
The answer to the question
[f(x) for x in xs if x is not None else '']
Here we have a problem with the structure of the list: for x in xs should be at the end of the expression.
Correct way:
[f(x) if x is not None else '' for x in xs]
Further reading:
Does Python have a ternary conditional operator?
The specific problem has already been solved in previous answers, so I will address the general idea of using conditionals inside list comprehensions.
Here is an example that shows how conditionals can be written inside a list comprehension:
X = [1.5, 2.3, 4.4, 5.4, 'n', 1.5, 5.1, 'a'] # Original list
# Extract non-strings from X to new list
X_non_str = [el for el in X if not isinstance(el, str)] # When using only 'if', put 'for' in the beginning
# Change all strings in X to 'b', preserve everything else as is
X_str_changed = ['b' if isinstance(el, str) else el for el in X] # When using 'if' and 'else', put 'for' in the end
Note that in the first list comprehension for X_non_str, the order is:
expression for item in iterable if condition
and in the last list comprehension for X_str_changed, the order is:
expression1 if condition else expression2 for item in iterable
I always find it hard to remember that expression1 has to be before if and expression2 has to be after else. My head wants both to be either before or after.
I guess it is designed like that because it resembles normal language, e.g. "I want to stay inside if it rains, else I want to go outside"
In plain English the two types of list comprehensions mentioned above could be stated as:
With only if:
extract_apple for apple in apple_box if apple_is_ripe
and with if/else
mark_apple if apple_is_ripe else leave_it_unmarked for apple in apple_box
One way:
def change(x):
if x is None:
return f(x)
else:
return ''
result = [change(x) for x in xs]
Although then you have:
result = map(change, xs)
Or you can use a lambda inline.
Here is another illustrative example:
>>> print(", ".join(["ha" if i else "Ha" for i in range(3)]) + "!")
Ha, ha, ha!
It exploits the fact that if i evaluates to False for 0 and to True for all other values generated by the function range(). Therefore the list comprehension evaluates as follows:
>>> ["ha" if i else "Ha" for i in range(3)]
['Ha', 'ha', 'ha']
[f(x) if x != None else '' for x in xs]
Syntax for list comprehension:
[item if condition else item for item in items]
[f(item) if condition else value for item in items]
[item if condition for item in items]
[value if condition else value1 if condition1 else value2]
The other solutions are great for a single if / else construct. However, ternary statements within list comprehensions are arguably difficult to read.
Using a function aids readability, but such a solution is difficult to extend or adapt in a workflow where the mapping is an input. A dictionary can alleviate these concerns:
xs = [None, 'This', 'is', 'a', 'filler', 'test', 'string', None]
d = {None: '', 'filler': 'manipulated'}
res = [d.get(x, x) for x in xs]
print(res)
['', 'This', 'is', 'a', 'manipulated', 'test', 'string', '']
It has to do with how the list comprehension is performed.
Keep in mind the following:
[ expression for item in list if conditional ]
Is equivalent to:
for item in list:
if conditional:
expression
Where the expression is in a slightly different format (think switching the subject and verb order in a sentence).
Therefore, your code [x+1 for x in l if x >= 45] does this:
for x in l:
if x >= 45:
x+1
However, this code [x+1 if x >= 45 else x+5 for x in l] does this (after rearranging the expression):
for x in l:
if x>=45: x+1
else: x+5
Make a list from items in an iterable
It seems best to first generalize all the possible forms rather than giving specific answers to questions. Otherwise, the reader won't know how the answer was determined. Here are a few generalized forms I thought up before I got a headache trying to decide if a final else' clause could be used in the last form.
[expression1(item) for item in iterable]
[expression1(item) if conditional1 for item in iterable]
[expression1(item) if conditional1 else expression2(item) for item in iterable]
[expression1(item) if conditional1 else expression2(item) for item in iterable if conditional2]
The value of item doesn't need to be used in any of the conditional clauses. A conditional3 can be used as a switch to either add or not add a value to the output list.
For example, to create a new list that eliminates empty strings or whitespace strings from the original list of strings:
newlist = [s for s in firstlist if s.strip()]
There isn't any need for ternary if/then/else. In my opinion your question calls for this answer:
row = [unicode((x or '').strip()) for x in row]
You can combine conditional logic in a comprehension:
ps = PorterStemmer()
stop_words_english = stopwords.words('english')
best = sorted(word_scores.items(), key=lambda x: x[1], reverse=True)[:10000]
bestwords = set([w for w, s in best])
def best_word_feats(words):
return dict([(word, True) for word in words if word in bestwords])
# with stemmer
def best_word_feats_stem(words):
return dict([(ps.stem(word), True) for word in words if word in bestwords])
# with stemmer and not stopwords
def best_word_feats_stem_stop(words):
return dict([(ps.stem(word), True) for word in words if word in bestwords and word not in stop_words_english])
# coding=utf-8
def my_function_get_list():
my_list = [0, 1, 2, 3, 4, 5]
# You may use map() to convert each item in the list to a string,
# and then join them to print my_list
print("Affichage de my_list [{0}]".format(', '.join(map(str, my_list))))
return my_list
my_result_list = [
(
number_in_my_list + 4, # Condition is False : append number_in_my_list + 4 in my_result_list
number_in_my_list * 2 # Condition is True : append number_in_my_list * 2 in my_result_list
)
[number_in_my_list % 2 == 0] # [Condition] If the number in my list is even
for number_in_my_list in my_function_get_list() # For each number in my list
]
print("Affichage de my_result_list [{0}]".format(', '.join(map(str, my_result_list))))
(venv) $ python list_comp.py
Affichage de my_list [0, 1, 2, 3, 4, 5]
Affichage de my_result_list [0, 5, 4, 7, 8, 9]
So, for you:
row = [('', unicode(x.strip()))[x is not None] for x in row]