Home > Net >  compare integer to value in list in python
compare integer to value in list in python

Time:11-10

I'm trying to compare a value in list to a integer. can someone help how to do that

list = [1]
if list == number:
    print(number)

I want some thing like above, how to do

CodePudding user response:

list = ["1","20","300","4000","50000"]
temp = int(list[x])
if temp == number:
    print(number)

#x = 1 to list# (number of values in list)

Be warned, if the string in the list isn't an integer (when removing "quotes"), it will throw an error

CodePudding user response:

if you have only one element in your list you can call him by index

list[0] == some_number:

if you have many elements you can sort through their by loop

for element in list:
    if element == some_number:
        print(some_number)
  • Related