#Code
x = [17, 15, 18, 21, 5, 6]
for y in x:
if y < 18:
y = x.copy()
print (y)
Im want:
18 21
but result:
17, 15, 18, 21, 5, 6
17, 15, 18, 21, 5, 6
17, 15, 18, 21, 5, 6
17, 15, 18, 21, 5, 6
>Solution :
Iterate over the values, if the value is valid (>=18) keep it in an other list, at the end print them
x = [17, 15, 18, 21, 5, 6]
valid_values = []
for y in x:
if y >= 18:
valid_values.append(y)
print(valid_values) # [18, 21]
With list-comprehension
valid_values = [y for y in x if y >= 18]