-1
I am trying to return only the even numbers of a list1=[4,3,2,5,7,6]
for the return in a Lista2=[2,4,6]
I tried the recursive method with for but returns the error: Typeerror: Unsupported operand type(s) for %: 'list' and 'int'
lista1 = [4,3,2,5,7,6]
lista2 = []
for lista2 in lista1:
if lista1 % 2 == 0:
print(lista2)
How about
lista2 = [ el for el in lista1 if el % 2 == 0 ]?– Jefferson Quesado
By the way, your function has nothing recursive. And you made a nice mess with the variables
– Jefferson Quesado
Thank you, Jefferson. By the way, when you say 'mess with variables' what do you mean? I appreciate immensely help, I am starting the studies in Python and any constructive criticism is welcome.
– edtech
You declare
lista2as an empty list. Then use as an iteration variable in the loopfor, but compares an operation with the variablelista1within the loop. That confusion.– Jefferson Quesado
You’re confusing very basic concepts needed to structure a program. Answering the question in a way that you would understand would amount to a "class 0" - I think the recommendation is that you look for some Python tutorial for beginners in programming, and read until they teach about "variables", "lists"".
– jsbueno
Show people, thanks for the tips!
– edtech