6: print(i) Since python interpreter can identify that" /> 6: print(i) Since python interpreter can identify that" /> 6: print(i) Since python interpreter can identify that"/>

Why I need to convert the integer values into string here

77 views Asked by At
lst1 = [77, "Pizza", 5 , 7, "Burger"]
print(type(lst1[0]), type(lst1[1]))

for i in lst1:
    if i > 6:
        print(i)

Since python interpreter can identify that which values of the list are integer and which values of the list are string, why in the if statement it is not working ?

Note: I am not asking the correct code, I just want to know why this code is not working ?

2

There are 2 answers

7
LiterallyGutsFromBerserk On

You just need to make sure the type is an integer, and then see if it's bigger than 6

lst1 = [77, "Pizza", 5 , 7, "Burger"]
print(type(lst1[0]), type(lst1[1]))

for i in lst1:
    if(type(i)==int):
        if i > 6:
            print(i)
0
jeekiii On

Let's ignore the ints in the array for a second and just run:

lst1 = ["Pizza", "Burger"]

for i in lst1:
    if i > 6:
        print(i)

This will fail because it cannot compare "Pizza" and 6. Adding integers in the array does not change this fact.

It knows they are different, but there is no ordering between strings and intergers, therefore it cannot compare "Pizza" and 6.