Listar compreensões em Python
[output_expression for variable in input_sequence if filter_condition]
Shy Skunk
[output_expression for variable in input_sequence if filter_condition]
S = [x**2 for x in range(10)]
V = [2**i for i in range(13)]
numbers = [1, 2, 3, 4, 5, 6]
squares = [i*i for i in numbers]
print(squares)
# List
# new_list[<action> for <item> in <iterator> if <some condition>]
a = [i for i in 'hello'] # ['h', 'e', 'l', 'l', '0']
b = [i*2 for i in [1,2,3]] # [2, 4, 6]
c = [i for i in range(0,10) if i % 2 == 0]# [0, 2, 4, 6, 8]
[output_expression for element in list if condition]Code language: Python (python)
l = [i for i in some_container]