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>You could do it a few different ways:</p>

<pre><code>set(serviceComponents) &lt;= set(data)
set(serviceComponents).issubset(data)
all(c in data for c in serviceComponents)
</code></pre>

<p>You can make it shorter, but you lose readability. What you have now is probably fine. I'd go with the first approach personally, since it expresses your intent clearly with set operations.</p>

python