+ 1

Just taking numbers from list

list=[1,"appel",2,3,"peach",4,5,"orange",6,"carrot",8,"banana",10,"cat"] for number in list: while number<=9: if (number>2): print(number) I just want to see on my output 3,4,5,6,8 these numbers. Where have i done a mistake?

10th Jun 2021, 5:08 PM
Habib
Habib - avatar
6 odpowiedzi
10th Jun 2021, 5:25 PM
Steven M
Steven M - avatar
+ 2
Calvin Thomas , there are 2 issues in your code sample: ▪︎ isnumeric() is a string function, but numbers in list are integers ▪︎ the result of the code is 1,2,3,4,5,6,8,10 but should be 3,4,5,6,8
10th Jun 2021, 5:42 PM
Lothar
Lothar - avatar
+ 1
Remove the while loop, unindent your if statement and print statement... it should work
10th Jun 2021, 5:23 PM
Steven M
Steven M - avatar
+ 1
The while loop makes no sense. Its exit condition depends on a variable which doesn't change inside the loop, so it either runs once or it runs forever. There's no filter to check only numbers. Things to learn that will help you a lot here: - break and continue statements - You can end a loop or skip an iteration at arbitrary conditions - List comprehensions - You can create a new list by filtering an existing one - join function - Easily formats lists, which helps printing them
12th Jun 2021, 1:28 AM
Emerson Prado
Emerson Prado - avatar
0
What is the need of while loop there in for loop . it loops forever. Just use a if condition instead. And you can check condition like if type(number) == int :
10th Jun 2021, 5:25 PM
Jayakrishna 🇮🇳
0
Habib Here's a possible solution: print(*filter(lambda x: x.isnumeric(), list), sep=',') # Hope this helps
10th Jun 2021, 5:32 PM
Calvin Thomas
Calvin Thomas - avatar