Search code examples
pythonkeykeyword-argument

how to check if integer which I want inside my value(if int is not iterable)


I am trying to run this test code:

def test(**kwargs):
    for key, value in kwargs.items():
        if 'sdk' in key:
            print value
            if 1 in value:
                print 'HelloWorld'    

test(sdk=1)

But of course it will give me error since integer is not iterable , so how do you guys check if integer which you want inside value? Thank you


Solution

  • The expression you actually should be using is if value == 1.

    if 1 in value expects that value is an iterable (e.g., an array or a dict) and returns True if 1 is contained in value, and False otherwise.

    If value is not an iterable, you'll get an exception:

    >>> arr = [1, 2, 3]
    >>> 1 in arr
    True
    >>> 4 in arr
    False
    >>> not_arr = 1
    >>> 1 in not_arr
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: argument of type 'int' is not iterable
    >>> not_arr == 1
    True
    >>> not_arr == 2
    False