3
How can I use comprehensilist on for that case:
>>> a = 'A'
>>> lista = [1, 2, 8, 5, 10]
>>> l = [a, num for num in lista if num%2 == 0]
File "<stdin>", line 1
l = [a, num for num in lista if num%2 == 0]
^
SyntaxError: invalid syntax
As you can see this syntax is not possible. My idea was to create a new list only with even numbers of lista
and with a
.
['A', 2, 8, 10]
How can I do that with comprehensilist on?
But what do you mean? You want
A
stay in the first item of the list and then even numbers?– Wallace Maxters
@Wallacemaxters, not exactly. The order doesn’t matter, I just want to add
a
on the list.– Matheus Saraiva
maybe something like:
l = [a] + [num for num in lista if num%2 == 0]
?– JJoao
@Jjoao gave the answer. Put as answer so I can mark.
– Matheus Saraiva