Check for a list in other lists?

Ok, I'm trying to go for a more pythonic method of doing things.

How can I do the following:

required_values = ['A','B','C']
some_map = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4}

for required_value in required_values:
    if not required_value in some_map:
        print 'It Doesnt Exists'
        return False
return True

I looked at the built-in function all, but I cannot figure out how to apply this to the above scenario.

Any suggestions for making it more pythonic?

+3
source share
3 answers
all(value in some_map for value in required_values)
+10
source
return set(required_values).issubset(set(some_map.keys()))
+3
source

:

return not bool ([x x required_values, x some_map.keys()]) ( bool)

return not [x x required_values, x some_map.keys()] ( , )

inside [] , , False, - True.

therefore, if the map does not have all the required values, at least one element will be in the list, constructed according to the expression of understanding the list. This will be evaluated as True, therefore we deny the result to fulfill your code requirements (all necessary values ​​must be present on the card).

0
source

Source: https://habr.com/ru/post/1709400/


All Articles