Validating that all components required for an object to exist are present
<p>I need to write a script that gets a list of components from an external source and based on a pre-defined list it validates whether the service is complete. This is needed because the presence of a single component doesn't automatically imply that the service is present - some components are pre-installed even when there is no service. I've devised something really simple below, but I was wondering what is the intelligent way of doing this? There must be a cleaner, simpler way.</p>

<pre><code># Components that make up a complete service
serviceComponents = ['A','B']
# Input from JSON
data = ['B','A','C']
serviceComplete = True
for i in serviceComponents:
    if i in data:
        print 'yay ' + i + ' found from ' + ', '.join(service2)
    else:
        serviceComplete = False
        break
# If serviceComplete = True do blabla...
</code></pre>

<p><a href="https://docs.python.org/2/library/stdtypes.html#set" rel="nofollow"><strong>Built-in Set</strong></a> would serve for you, use <a href="https://docs.python.org/2/library/stdtypes.html#set.issubset" rel="nofollow">set.issubset</a> to identify that your required service components is subset of input data:</p>

<pre><code>serviceComponents = set(['A','B'])
input_data = set(['B','A','C'])
if serviceComponents.issubset(input_data):
    # perform actions ...
</code></pre>

python