how to put variables 'a', 'b' and 'c' inside list 'Var2'? in python

Viewed 49
a=0
b=1
c=2
d=3
e=4

Var = [a,b,c,d,e]
Var2 =[]

for i in Var:
    if i <= 2:
    Var2 = [i]
print(Var2)

this is the result

[2]

Process finished with exit code 0

2 Answers

Replcae Var2 = [i] with:

Var2 += [i]

or you can use Var2.append(i)

Use append() function of list.

for i in Var:
    if i <= 2:
        Var2.append(i)
print(Var2)
Related