Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm new to python, If the number from the list is less than 18, then the number is not printed

#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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading