xxxxxxxxxx
# List comprehension
list_comp = [i+3 for i in range(20)]
# above code is similar to
for i in range(20):
print(i + 3)
xxxxxxxxxx
nums = [4, -7, 9, 1, -1, 8, -6]
half_of_nums = [x/2 for x in nums] #[2, -3.5, 4.5, 0.5, -0.5, 4, -3]
#optionally you can add an if statement like this
half_of_positive_nums = [x/2 for x in nums if x>=0] #[2, 4.5, 0.5, 4]
xxxxxxxxxx
# Normal List
new_list = [item + 1 for item in some_list]
# Normal List with conditional
new_list1 = [item ** 2 for item in range(10) if item % 2 == 0] # "if" evaluation is on right if no "else" is present
new_list2 = [ item ** 2 if item % 2 == 0 else 0 for item in range(10)] # "if" evaluation is on left if "else" is present
# Nested List
new_list = [(outer_loop_val, inner_loop_val) for outer_loop_val in range(0, 2) for inner_loop_val in range(6, 8)]
xxxxxxxxxx
#example: removing common elements found in `a` from `b`.
a = [1,2,3,4,5]
b = [5,6,7,8,9]
# desired output: [1,2,3,4]
# gets each item found in `a` AND not in `b`
print([i for i in a if i not in b])
xxxxxxxxxx
Yes:
result = [mapping_expr for value in iterable if filter_expr]
result = [{'key': value} for value in iterable
if a_long_filter_expression(value)]
result = [complicated_transform(x)
for x in iterable if predicate(x)]
descriptive_name = [
transform({'key': key, 'value': value}, color='black')
for key, value in generate_iterable(some_input)
if complicated_condition_is_met(key, value)
]
result = []
for x in range(10):
for y in range(5):
if x * y > 10:
result.append((x, y))
return {x: complicated_transform(x)
for x in long_generator_function(parameter)
if x is not None}
squares_generator = (x**2 for x in range(10))
unique_names = {user.name for user in users if user is not None}
eat(jelly_bean for jelly_bean in jelly_beans
if jelly_bean.color == 'black')
xxxxxxxxxx
# Python program to demonstrate list
# comprehension in Python
# below list contains square of all
# odd numbers from range 1 to 10
odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]
print (odd_square)
xxxxxxxxxx
Yes:
result = [mapping_expr for value in iterable if filter_expr]
result = [{'key': value} for value in iterable
if a_long_filter_expression(value)]
result = [complicated_transform(x)
for x in iterable if predicate(x)]
descriptive_name = [
transform({'key': key, 'value': value}, color='black')
for key, value in generate_iterable(some_input)
if complicated_condition_is_met(key, value)
]
result = []
for x in range(10):
for y in range(5):
if x * y > 10:
result.append((x, y))
return {x: complicated_transform(x)
for x in long_generator_function(parameter)
if x is not None}
squares_generator = (x**2 for x in range(10))
unique_names = {user.name for user in users if user is not None}
eat(jelly_bean for jelly_bean in jelly_beans
if jelly_bean.color == 'black')
xxxxxxxxxx
new_list = [<Conditional Expression> for <item> in <iterable>]
new_list = [<Exp1> if condition else <Exp2> if condition else <Exp3> for <item> in <iterable>]
xxxxxxxxxx
lst=[1,2,3,4,5]
lst2=[item for item in lst if <condition>]
# generates a list based on another list and an if statement. the code above is a replacement for:
lst=[1,2,3,4,5]
lst2=[]
for item in lst:
if <condition>:
lst2.append(item)
xxxxxxxxxx
[output_expression for variable in input_sequence if filter_condition]